1 19 package org.netbeans.modules.j2ee.sun.share.configbean; 20 21 import java.awt.event.ItemEvent ; 22 import java.lang.reflect.Array ; 23 import java.util.ArrayList ; 24 import java.util.List ; 25 import java.util.ResourceBundle ; 26 import java.net.URL ; 27 import java.net.URLClassLoader ; 28 import java.text.MessageFormat ; 29 import java.util.Iterator ; 30 import javax.swing.SwingUtilities ; 31 32 import javax.enterprise.deploy.spi.exceptions.ConfigurationException ; 33 34 import org.openide.util.HelpCtx; 35 import org.openide.util.Lookup; 36 import org.netbeans.api.javahelp.Help; 37 import org.netbeans.modules.j2ee.sun.dd.api.CommonDDBean; 38 import org.openide.ErrorManager; 39 40 41 45 public class Utils implements org.netbeans.modules.j2ee.sun.share.Constants { 46 47 48 private Utils() { 49 } 50 51 private static final String KEYSEPSTR = "|"; 53 private static ResourceBundle ubundle = 54 ResourceBundle.getBundle("org.netbeans.modules.j2ee.sun.share.configbean.Bundle"); 56 public static String getFQNKey(String uri, String fname) { 57 String key = ""; 58 if (null != uri) 59 key += uri; 60 if (key.indexOf(KEYSEPSTR)>-1) 61 throw new IllegalArgumentException ("uri"); 62 key += KEYSEPSTR + fname; 63 assert !key.equals(KEYSEPSTR); 64 return key; 65 } 66 67 public static String getUriFromKey(String key) { 68 String uri = ""; 69 int sepLocation = key.indexOf(KEYSEPSTR); 70 if (sepLocation > 1) 71 uri = key.substring(0,sepLocation); 72 return uri; 73 } 74 75 public static String getFilenameFromKey(String key) { 76 int sepLocation = key.indexOf(KEYSEPSTR); 77 String fname = key.substring(sepLocation+1); 80 return fname; 81 } 82 83 public static boolean notEmpty(String testedString) { 84 return (testedString != null) && (testedString.length() > 0); 85 } 86 87 public static boolean strEmpty(String testedString) { 88 return testedString == null || testedString.length() == 0; 89 } 90 91 public static boolean strEquals(String one, String two) { 92 boolean result = false; 93 94 if(one == null) { 95 result = (two == null); 96 } else { 97 if(two == null) { 98 result = false; 99 } else { 100 result = one.equals(two); 101 } 102 } 103 return result; 104 } 105 106 public static boolean strEquivalent(String one, String two) { 107 boolean result = false; 108 109 if(strEmpty(one) && strEmpty(two)) { 110 result = true; 111 } else if(one != null && two != null) { 112 result = one.equals(two); 113 } 114 115 return result; 116 } 117 118 public static int strCompareTo(String one, String two) { 119 int result; 120 121 if(one == null) { 122 if(two == null) { 123 result = 0; 124 } else { 125 result = -1; 126 } 127 } else { 128 if(two == null) { 129 result = 1; 130 } else { 131 result = one.compareTo(two); 132 } 133 } 134 135 return result; 136 } 137 138 139 public static boolean hasTrailingSlash(String path) { 140 return (path.charAt(path.length()-1) == '/'); 141 } 142 143 public static boolean containsWhitespace(String data) { 144 boolean result = false; 145 146 if(notEmpty(data)) { 147 for(int i = 0, datalength = data.length(); i < datalength; i++) { 148 if(Character.isSpaceChar(data.charAt(i))) { 149 result = true; 150 break; 151 } 152 } 153 } 154 155 return result; 156 } 157 158 public static boolean isJavaIdentifier(final String id) { 159 boolean result = true; 160 161 if(!notEmpty(id) || !Character.isJavaIdentifierStart(id.charAt(0))) { 162 result = false; 163 } else { 164 for(int i = 1, idlength = id.length(); i < idlength; i++) { 165 if(!Character.isJavaIdentifierPart(id.charAt(i))) { 166 result = false; 167 break; 168 } 169 } 170 } 171 172 return result; 173 } 174 175 public static boolean isJavaPackage(final String pkg) { 176 boolean result = false; 177 178 if(notEmpty(pkg)) { 179 int state = 0; 180 for(int i = 0, pkglength = pkg.length(); i < pkglength && state < 2; i++) { 181 switch(state) { 182 case 0: 183 if(Character.isJavaIdentifierStart(pkg.charAt(i))) { 184 state = 1; 185 } else { 186 state = 2; 187 } 188 break; 189 case 1: 190 if(pkg.charAt(i) == '.') { 191 state = 0; 192 } else if(!Character.isJavaIdentifierPart(pkg.charAt(i))) { 193 state = 2; 194 } 195 break; 196 } 197 } 198 199 if(state == 1) { 200 result = true; 201 } 202 } 203 204 return result; 205 } 206 207 public static boolean isJavaClass(final String cls) { 208 return isJavaPackage(cls); 209 } 210 211 public static CommonDDBean [] listToArray(List list, Class targetClass) { 212 CommonDDBean [] result = null; 213 if(list != null) { 214 int size = list.size(); 215 if(size != 0) { 216 result = (CommonDDBean []) Array.newInstance(targetClass, size); 217 for(int i = 0; i < size; i++) { 218 CommonDDBean property = (CommonDDBean) list.get(i); 219 result[i] = (CommonDDBean) property.clone(); 220 } 221 } 222 } 223 return result; 224 } 225 226 public static CommonDDBean [] listToArray(List list, Class targetClass, String newVersion) { 227 CommonDDBean [] result = null; 228 if(list != null) { 229 int size = list.size(); 230 if(size != 0) { 231 result = (CommonDDBean []) Array.newInstance(targetClass, size); 232 for(int i = 0; i < size; i++) { 233 CommonDDBean property = (CommonDDBean) list.get(i); 234 result[i] = (CommonDDBean) property.cloneVersion(newVersion); 235 } 236 } 237 } 238 return result; 239 } 240 241 public static List arrayToList(CommonDDBean[] beans) { 242 List result = null; 243 244 if(beans != null && beans.length > 0) { 245 result = new ArrayList (beans.length+3); 246 for(int i = 0; i < beans.length; i++) { 247 result.add(beans[i]); 248 } 249 } 250 251 return result; 252 } 253 254 private static final String [] booleanStrings = { 255 "0", "1", "false", "true", "no", "yes", "off", "on" }; 260 261 public static boolean booleanValueOf(String val) { 262 boolean result = false; 263 int valueIndex = -1; 264 265 if(val != null && val.length() > 0) { 266 val = val.trim(); 267 for(int i = 0; i < booleanStrings.length; i++) { 268 if(val.compareToIgnoreCase(booleanStrings[i]) == 0) { 269 valueIndex = i; 270 break; 271 } 272 } 273 } 274 275 if(valueIndex >= 0) { 276 if(valueIndex%2 == 1) { 277 result = true; 278 } 279 } 280 281 return result; 282 } 283 284 public static String encodeUrlField(String url) { 285 String encodedUrl = url; 286 287 if(encodedUrl != null) { 290 encodedUrl = encodedUrl.replace (' ', '_'); } 292 293 if(encodedUrl != null) { try { 296 StringBuffer result = new StringBuffer (encodedUrl.length() + 10); 297 String s[] = encodedUrl.split("/"); for(int i = 0; i < s.length; i++) { 299 result.append(java.net.URLEncoder.encode(s[i], "UTF-8")); if(i != s.length - 1) { 301 result.append("/"); } 303 } 304 encodedUrl = result.toString(); 305 } catch (Exception ex){ 306 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); 308 } 309 } 310 311 return encodedUrl; 312 } 313 314 public static URL getResourceURL(String resource, Class relatedClass) { 315 URL result = null; 316 ClassLoader classLoader = relatedClass.getClassLoader(); 317 318 if(classLoader instanceof java.net.URLClassLoader ) { 319 URLClassLoader urlClassLoader = (java.net.URLClassLoader ) classLoader; 320 result = urlClassLoader.findResource(resource); 321 } 322 else { 323 result = classLoader.getResource(resource); 324 } 325 326 return result; 327 } 328 329 public static void invokeHelp(String helpId) { 330 invokeHelp(new HelpCtx(helpId)); 331 } 332 333 public static void invokeHelp(final HelpCtx helpCtx) { 334 SwingUtilities.invokeLater(new Runnable () { 335 public void run() { 336 ((Help) Lookup.getDefault().lookup(Help.class)).showHelp(helpCtx); 337 } 338 }); 339 } 340 341 public static boolean interpretCheckboxState(ItemEvent e) { 342 boolean state = false; 343 344 if(e.getStateChange() == ItemEvent.SELECTED) { 345 state = true; 346 } else if(e.getStateChange() == ItemEvent.DESELECTED) { 347 state = false; 348 } 349 350 return state; 351 } 352 353 356 static String getDefaultCmpResourceJndiName(EjbJarRoot jarDCB) { 357 return "jdo/pmf"; 358 } 359 360 static ConfigurationException makeCE(String messageKey, Object [] params, Throwable cause) { 361 String format = null; 362 boolean poorFormat = false; 363 try { 364 format = ubundle.getString(messageKey); 365 } 366 catch (RuntimeException re) { 367 poorFormat = true; 368 jsr88Logger.throwing(Utils.class.getName(), "makeCE", re); 369 format = ubundle.getString("DEF_ConfigurationExceptionFormat"); 370 int len = 1; 371 if (null != params) 372 len += params.length; 373 Object tparams[] = new Object [len]; 374 tparams[0] = messageKey; 375 for (int i = 1; i < len; i++) { 376 tparams[i] = params[i-1]; 377 } 378 params = tparams; 379 } 380 381 String message = MessageFormat.format(format, params); 382 ConfigurationException retVal = new ConfigurationException (message); 383 if (null != cause) { 384 retVal.initCause(cause); 385 } 386 if (poorFormat) 387 jsr88Logger.severe(message); 388 return retVal; 389 } 390 391 400 public static ServiceRef findServiceRef(SunONEDeploymentConfiguration config, String serviceRefName) { 401 ServiceRef result = null; 402 BaseRoot rootDCB = config.getMasterDCBRoot(); 403 404 if(rootDCB instanceof EjbJarRoot) { 405 Iterator childIter = rootDCB.getChildren().iterator(); 406 while(childIter.hasNext() && result == null) { 407 Object child = childIter.next(); 408 if(child instanceof BaseEjb) { 409 Iterator subChildIter = ((BaseEjb) child).getChildren().iterator(); 410 while(subChildIter.hasNext() && result == null) { 411 Object subChild = subChildIter.next(); 412 if(subChild instanceof ServiceRef) { 413 ServiceRef serviceRef = (ServiceRef) subChild; 414 if(serviceRefName.equals(serviceRef.getServiceRefName())) { 415 result = serviceRef; 416 } 417 } 418 } 419 } 420 } 421 } else { 422 Iterator childIter = rootDCB.getChildren().iterator(); 423 while(childIter.hasNext()) { 424 Object child = childIter.next(); 425 if(child instanceof ServiceRef) { 426 ServiceRef serviceRef = (ServiceRef) child; 427 if(serviceRefName.equals(serviceRef.getServiceRefName())) { 428 result = serviceRef; 429 break; 430 } 431 } 432 } 433 } 434 435 return result; 436 } 437 } 438 | Popular Tags |