This project has retired. For details please refer to its Attic page.
UddiEntityPublisher xref
View Javadoc
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 at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * 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 and
14   * limitations under the License.
15   *
16   */
17  
18  package org.apache.juddi.model;
19  
20  import java.util.List;
21  import java.util.StringTokenizer;
22  import java.util.Vector;
23  import java.util.logging.Level;
24  import java.util.logging.Logger;
25  
26  import javax.persistence.Column;
27  import javax.persistence.EntityManager;
28  import javax.persistence.Id;
29  import javax.persistence.MappedSuperclass;
30  import javax.persistence.Query;
31  import javax.persistence.Transient;
32  import org.apache.juddi.config.AppConfig;
33  import org.apache.juddi.config.Property;
34  
35  import org.apache.juddi.keygen.KeyGenerator;
36  import org.apache.juddi.query.util.DynamicQuery;
37  import org.apache.juddi.validation.ValidateUDDIKey;
38  import org.uddi.v3_service.DispositionReportFaultMessage;
39  
40  /**
41   * @author <a href="mailto:jfaath@apache.org">Jeff Faath</a>
42   */
43  @MappedSuperclass
44  public class UddiEntityPublisher {
45  	
46          private transient static final Logger logger = Logger.getLogger(UddiEntityPublisher.class.getCanonicalName());
47  	protected String authorizedName;
48  	private List<String> keyGeneratorKeys = null;
49  
50  	public UddiEntityPublisher() {
51  	}
52  	
53  	public UddiEntityPublisher(String authorizedName) {
54  		this.authorizedName = authorizedName;
55  	}
56  	
57  	@Id
58  	@Column(name = "authorized_name", nullable = false, length = 255)
59  	public String getAuthorizedName() {
60  		return this.authorizedName;
61  	}
62  	public void setAuthorizedName(String authorizedName) {
63  		this.authorizedName = authorizedName;
64  	}
65  	
66  	@Transient
67  	public List<String> getKeyGeneratorKeys() {
68  		return keyGeneratorKeys;
69  	}
70  	public void setKeyGeneratorKeys(List<String> keyGeneratorKeys) {
71  		this.keyGeneratorKeys = keyGeneratorKeys;
72  	}
73  
74  	@SuppressWarnings("unchecked")
75  	public void populateKeyGeneratorKeys(EntityManager em) {
76  		DynamicQuery getKeysQuery = new DynamicQuery();
77  		getKeysQuery.append("select t.entityKey from Tmodel t").pad().WHERE().pad();
78  
79  		DynamicQuery.Parameter pubParam = new DynamicQuery.Parameter("t.authorizedName", 
80  				 getAuthorizedName(), 
81  				 DynamicQuery.PREDICATE_EQUALS);
82  
83  		DynamicQuery.Parameter keyParam = new DynamicQuery.Parameter("UPPER(t.entityKey)", 
84  				 (DynamicQuery.WILDCARD + KeyGenerator.KEYGENERATOR_SUFFIX).toUpperCase(), 
85  				 DynamicQuery.PREDICATE_LIKE);
86  		
87  		
88  		getKeysQuery.appendGroupedAnd(pubParam, keyParam);
89  		Query qry = getKeysQuery.buildJPAQuery(em);
90  		
91  		keyGeneratorKeys = qry.getResultList();
92  	}
93  	
94          /**
95           * Determines if *this publisher owns a specific key
96           * @param entity
97           * @return true/false
98           */
99  	public boolean isOwner(UddiEntity entity){
100             try {
101                 AppConfig instance = AppConfig.getInstance();
102 
103                 if (entity != null) {
104                     if (entity.getAuthorizedName().equals(getAuthorizedName())
105                             && entity.getNodeId().equals((AppConfig.getConfiguration().getString(Property.JUDDI_NODE_ID)))) {
106                         return true;
107                     }
108                 }
109             } catch (Exception ex) {
110                 logger.log(Level.WARNING, "Error caught determining node id! Defaulting to access denied", ex);
111             }
112             return false;
113 	}
114 
115 	
116 	public boolean isValidPublisherKey(EntityManager em, String key) {
117 		if (key == null)
118 			return false;
119 		
120 		if (keyGeneratorKeys == null)
121 			populateKeyGeneratorKeys(em);
122 		
123 		if (! key.contains(KeyGenerator.PARTITION_SEPARATOR)) return true; //v2 style key
124 		String keyPartition = key.substring(0, key.lastIndexOf(KeyGenerator.PARTITION_SEPARATOR));
125 		
126 		for (String keyGenKey : keyGeneratorKeys) {
127 			String keyGenPartition = keyGenKey.substring(0, keyGenKey.lastIndexOf(KeyGenerator.PARTITION_SEPARATOR));
128 			if (keyGenPartition.equalsIgnoreCase(keyPartition))
129 				return true;
130 		}
131 		return false;
132 	}
133 	
134 	/*
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 generator
136 	 * and all its sub-partitions are not already taken by another publisher.
137 	 */
138 	public boolean isKeyGeneratorAvailable(EntityManager em, String keygenKey) throws DispositionReportFaultMessage {
139 
140 		// First make sure the key is a valid UDDIv3 key per the specification's rules
141 		ValidateUDDIKey.validateUDDIv3KeyGeneratorKey(keygenKey);
142 		
143 		String partition = keygenKey.toUpperCase().substring(0, keygenKey.lastIndexOf(KeyGenerator.PARTITION_SEPARATOR));
144 		
145 		StringTokenizer tokenizer = new StringTokenizer(partition, KeyGenerator.PARTITION_SEPARATOR);
146 		int tokenCount = tokenizer.countTokens();
147 		// Must have 2 or more tokens as the first is the uddi scheme and the second is the domain key.
148 		if (tokenCount < 2)
149 			return false;
150 
151 		String domainPartition = (String)tokenizer.nextElement() + KeyGenerator.PARTITION_SEPARATOR + (String)tokenizer.nextElement();
152 		
153 		// 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 another
155 		// publisher has any of these partitions since if they do, current publisher won't have them.
156 		if (tokenCount > 2) {
157 			Vector<DynamicQuery.Parameter> params = new Vector<DynamicQuery.Parameter>(0);
158 
159 			DynamicQuery.Parameter pubParam = new DynamicQuery.Parameter("t.authorizedName", 
160 					 getAuthorizedName(), 
161 					 DynamicQuery.PREDICATE_EQUALS);
162 			
163 			int 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++;
168 			
169 			String subPartition = domainPartition;
170 			while (tokenizer.hasMoreElements()) {
171 				// Don't need to add the last token as it is the proposed key generator.
172 				if (tokenizer.countTokens() == 1)
173 					break;
174 
175 				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 			}
183 			
184 			DynamicQuery checkParentKeyQry = new DynamicQuery();
185 			checkParentKeyQry.append("select COUNT(t.entityKey) from Tmodel t").pad();
186 
187 			checkParentKeyQry.WHERE().pad().appendGroupedAnd(pubParam);
188 			checkParentKeyQry.AND().pad().appendGroupedOr(params.toArray(new DynamicQuery.Parameter[0]));
189 			
190 			Query qry = checkParentKeyQry.buildJPAQuery(em);			
191 			Number resultCount = (Number)qry.getSingleResult();
192 			if (resultCount.longValue() != requiredCount)
193 				return false;
194 		}
195 		else {
196 			// If only two tokens, then a domain key generator is being checked.  A domain key generator can only be registered if no other publishers
197 			// 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);
201 
202 			DynamicQuery.Parameter keyParam = new DynamicQuery.Parameter("UPPER(t.entityKey)", 
203 					(domainPartition + KeyGenerator.PARTITION_SEPARATOR + KeyGenerator.KEYGENERATOR_SUFFIX).toUpperCase(), 
204 					DynamicQuery.PREDICATE_EQUALS);
205 			
206 			DynamicQuery checkDomainKeyQry = new DynamicQuery();
207 			checkDomainKeyQry.append("select t.entityKey from Tmodel t").pad();
208 			
209 			checkDomainKeyQry.WHERE().pad().appendGroupedAnd(notPubParam, keyParam);
210 
211 			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 unavailable
214 			if (obj != null && obj.size() > 0)
215 				return false;
216 		}
217 		
218 		return true;
219 	}
220 
221 }