1/*2 * Copyright 2001-2008 The Apache Software Foundation.3 * 4 * Licensed under the Apache License, Version 2.0 (the "License");5 * you may not use this file except in compliance with the License.6 * You may obtain a copy of the License at7 * 8 * http://www.apache.org/licenses/LICENSE-2.09 * 10 * Unless required by applicable law or agreed to in writing, software11 * distributed under the License is distributed on an "AS IS" BASIS,12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.13 * See the License for the specific language governing permissions and14 * limitations under the License.15 *16 */1718package org.apache.juddi.model;
1920import java.util.List;
21import java.util.StringTokenizer;
22import java.util.Vector;
23import java.util.logging.Level;
24import java.util.logging.Logger;
2526import javax.persistence.Column;
27import javax.persistence.EntityManager;
28import javax.persistence.Id;
29import javax.persistence.MappedSuperclass;
30import javax.persistence.Query;
31import javax.persistence.Transient;
32import org.apache.juddi.config.AppConfig;
33import org.apache.juddi.config.Property;
3435import org.apache.juddi.keygen.KeyGenerator;
36import org.apache.juddi.query.util.DynamicQuery;
37import org.apache.juddi.validation.ValidateUDDIKey;
38import org.uddi.v3_service.DispositionReportFaultMessage;
3940/**41 * @author <a href="mailto:jfaath@apache.org">Jeff Faath</a>42 */43 @MappedSuperclass
44publicclassUddiEntityPublisher {
4546privatetransientstaticfinal Logger logger = Logger.getLogger(UddiEntityPublisher.class.getCanonicalName());
47protected String authorizedName;
48private List<String> keyGeneratorKeys = null;
4950publicUddiEntityPublisher() {
51 }
5253publicUddiEntityPublisher(String authorizedName) {
54this.authorizedName = authorizedName;
55 }
5657 @Id
58 @Column(name = "authorized_name", nullable = false, length = 255)
59public String getAuthorizedName() {
60returnthis.authorizedName;
61 }
62publicvoid setAuthorizedName(String authorizedName) {
63this.authorizedName = authorizedName;
64 }
6566 @Transient
67public List<String> getKeyGeneratorKeys() {
68return keyGeneratorKeys;
69 }
70publicvoid setKeyGeneratorKeys(List<String> keyGeneratorKeys) {
71this.keyGeneratorKeys = keyGeneratorKeys;
72 }
7374 @SuppressWarnings("unchecked")
75publicvoid populateKeyGeneratorKeys(EntityManager em) {
76 DynamicQuery getKeysQuery = newDynamicQuery();
77 getKeysQuery.append("select t.entityKey from Tmodel t").pad().WHERE().pad();
7879 DynamicQuery.Parameter pubParam = new DynamicQuery.Parameter("t.authorizedName",
80 getAuthorizedName(),
81 DynamicQuery.PREDICATE_EQUALS);
8283 DynamicQuery.Parameter keyParam = new DynamicQuery.Parameter("UPPER(t.entityKey)",
84 (DynamicQuery.WILDCARD + KeyGenerator.KEYGENERATOR_SUFFIX).toUpperCase(),
85 DynamicQuery.PREDICATE_LIKE);
868788 getKeysQuery.appendGroupedAnd(pubParam, keyParam);
89 Query qry = getKeysQuery.buildJPAQuery(em);
9091 keyGeneratorKeys = qry.getResultList();
92 }
9394/**95 * Determines if *this publisher owns a specific key96 * @param entity97 * @return true/false98 */99publicboolean isOwner(UddiEntity entity){
100try {
101AppConfig instance = AppConfig.getInstance();
102103if (entity != null) {
104if (entity.getAuthorizedName().equals(getAuthorizedName())
105 && entity.getNodeId().equals((AppConfig.getConfiguration().getString(Property.JUDDI_NODE_ID)))) {
106returntrue;
107 }
108 }
109 } catch (Exception ex) {
110 logger.log(Level.WARNING, "Error caught determining node id! Defaulting to access denied", ex);
111 }
112return false;
113 }
114115116publicboolean isValidPublisherKey(EntityManager em, String key) {
117if (key == null)
118return false;
119120if (keyGeneratorKeys == null)
121 populateKeyGeneratorKeys(em);
122123if (! key.contains(KeyGenerator.PARTITION_SEPARATOR)) returntrue; //v2 style key124 String keyPartition = key.substring(0, key.lastIndexOf(KeyGenerator.PARTITION_SEPARATOR));
125126for (String keyGenKey : keyGeneratorKeys) {
127 String keyGenPartition = keyGenKey.substring(0, keyGenKey.lastIndexOf(KeyGenerator.PARTITION_SEPARATOR));
128if (keyGenPartition.equalsIgnoreCase(keyPartition))
129returntrue;
130 }
131return false;
132 }
133134/*135 * This method will check if the given key generator key is available for this publisher. The idea is to make sure that the key generator136 * and all its sub-partitions are not already taken by another publisher.137 */138publicboolean isKeyGeneratorAvailable(EntityManager em, String keygenKey) throws DispositionReportFaultMessage {
139140// First make sure the key is a valid UDDIv3 key per the specification's rules141 ValidateUDDIKey.validateUDDIv3KeyGeneratorKey(keygenKey);
142143 String partition = keygenKey.toUpperCase().substring(0, keygenKey.lastIndexOf(KeyGenerator.PARTITION_SEPARATOR));
144145 StringTokenizer tokenizer = new StringTokenizer(partition, KeyGenerator.PARTITION_SEPARATOR);
146int tokenCount = tokenizer.countTokens();
147// Must have 2 or more tokens as the first is the uddi scheme and the second is the domain key.148if (tokenCount < 2)
149return false;
150151 String domainPartition = (String)tokenizer.nextElement() + KeyGenerator.PARTITION_SEPARATOR + (String)tokenizer.nextElement();
152153// If three or more tokens then we need to make sure the current publisher has the parent partitions. For example, you can't register the 154// uddi:domain:abc:123 key generator without having the uddi:domain and uddi:domain:abc key generators. This implicitly checks if another155// publisher has any of these partitions since if they do, current publisher won't have them.156if (tokenCount > 2) {
157 Vector<DynamicQuery.Parameter> params = new Vector<DynamicQuery.Parameter>(0);
158159 DynamicQuery.Parameter pubParam = new DynamicQuery.Parameter("t.authorizedName",
160 getAuthorizedName(),
161 DynamicQuery.PREDICATE_EQUALS);
162163int requiredCount = 0;
164 params.add(new DynamicQuery.Parameter("UPPER(t.entityKey)",
165 (domainPartition + KeyGenerator.PARTITION_SEPARATOR + KeyGenerator.KEYGENERATOR_SUFFIX).toUpperCase(),
166 DynamicQuery.PREDICATE_EQUALS));
167 requiredCount++;
168169 String subPartition = domainPartition;
170while (tokenizer.hasMoreElements()) {
171// Don't need to add the last token as it is the proposed key generator.172if (tokenizer.countTokens() == 1)
173break;
174175 String nextToken = (String)tokenizer.nextElement();
176 subPartition = subPartition + KeyGenerator.PARTITION_SEPARATOR + nextToken;
177 DynamicQuery.Parameter param = new DynamicQuery.Parameter("UPPER(t.entityKey)",
178 (subPartition + KeyGenerator.PARTITION_SEPARATOR + KeyGenerator.KEYGENERATOR_SUFFIX).toUpperCase(),
179 DynamicQuery.PREDICATE_EQUALS);
180 params.add(param);
181 requiredCount++;
182 }
183184 DynamicQuery checkParentKeyQry = newDynamicQuery();
185 checkParentKeyQry.append("select COUNT(t.entityKey) from Tmodel t").pad();
186187 checkParentKeyQry.WHERE().pad().appendGroupedAnd(pubParam);
188 checkParentKeyQry.AND().pad().appendGroupedOr(params.toArray(new DynamicQuery.Parameter[0]));
189190 Query qry = checkParentKeyQry.buildJPAQuery(em);
191 Number resultCount = (Number)qry.getSingleResult();
192if (resultCount.longValue() != requiredCount)
193return false;
194 }
195else {
196// If only two tokens, then a domain key generator is being checked. A domain key generator can only be registered if no other publishers197// own it. For example, if trying to register the uddi:domain:abc:123 key then uddi:domain cannot be owned by another publisher.198 DynamicQuery.Parameter notPubParam = new DynamicQuery.Parameter("t.authorizedName",
199 getAuthorizedName(),
200 DynamicQuery.PREDICATE_NOTEQUALS);
201202 DynamicQuery.Parameter keyParam = new DynamicQuery.Parameter("UPPER(t.entityKey)",
203 (domainPartition + KeyGenerator.PARTITION_SEPARATOR + KeyGenerator.KEYGENERATOR_SUFFIX).toUpperCase(),
204 DynamicQuery.PREDICATE_EQUALS);
205206 DynamicQuery checkDomainKeyQry = newDynamicQuery();
207 checkDomainKeyQry.append("select t.entityKey from Tmodel t").pad();
208209 checkDomainKeyQry.WHERE().pad().appendGroupedAnd(notPubParam, keyParam);
210211 Query qry = checkDomainKeyQry.buildJPAQuery(em);
212 List<?> obj = qry.getResultList();
213// If there are results then another publisher has the domain key and therefore the key generator is unavailable214if (obj != null && obj.size() > 0)
215return false;
216 }
217218returntrue;
219 }
220221 }