1 19 20 package org.netbeans.modules.j2ee.sun.ide.j2ee; 21 22 import java.io.File ; 23 import java.io.IOException ; 24 import java.io.FileInputStream ; 25 import java.util.Collection ; 26 import java.util.Enumeration ; 27 import java.util.Properties ; 28 import java.util.logging.Level ; 29 30 import org.netbeans.modules.j2ee.deployment.plugins.api.InstanceProperties; 31 import org.netbeans.modules.j2ee.sun.api.Asenv; 32 import org.netbeans.modules.j2ee.sun.ide.Installer; 33 import org.netbeans.modules.j2ee.sun.api.ServerLocationManager; 34 import org.netbeans.modules.j2ee.sun.api.SunURIManager; 35 import org.netbeans.modules.j2ee.sun.ide.editors.CharsetDisplayPreferenceEditor; 36 import org.netbeans.modules.j2ee.sun.ide.j2ee.ui.Util; 37 import org.netbeans.modules.j2ee.sun.share.SecurityMasterListModel; 38 import org.netbeans.modules.j2ee.sun.share.CharsetMapping; 39 import org.openide.ErrorManager; 40 import org.openide.filesystems.FileLock; 41 import org.openide.filesystems.FileObject; 42 import org.openide.filesystems.FileSystem; 43 import org.openide.filesystems.FileUtil; 44 import org.openide.filesystems.Repository; 45 import org.openide.util.Lookup; 46 import org.openide.util.NbBundle; 47 48 52 public class PluginProperties { 53 54 java.util.logging.Logger jsr88Logger = 55 java.util.logging.Logger.getLogger("com.sun.enterprise.tools.jsr88.spi"); 56 57 private String logLevel = null; 58 private boolean incrementalDeployPossible = true; 60 private FileObject propertiesFile = null; 61 63 private static final String PLUGIN_PROPERTIES_VERSION = "version"; 65 68 private static final String PLUGIN_CURRENT_VERSION = "5.5"; private static final String INCREMENTAL = "incrementalDeploy"; private static final String PRINCIPAL_PREFIX = "principalEntry."; private static final String GROUP_PREFIX = "groupEntry."; private static final String LOG_LEVEL_KEY = "logLevel"; private static final String CHARSET_DISP_PREF_KEY = "charsetDisplayPreference"; public static final String INSTALL_ROOT_PROP_NAME = "com.sun.aas.installRoot"; 76 public static final String COBUNDLE_DEFAULT_INSTALL_PATH ="AS9.0"; public static final String COBUNDLE_DEFAULT_INSTALL_PATH2 ="AS8.2"; 79 80 81 static private PluginProperties thePluginProperties=null; 82 83 public static PluginProperties getDefault(){ 84 if (thePluginProperties==null) { 85 thePluginProperties= new PluginProperties(); 86 } 87 return thePluginProperties; 88 } 89 90 91 92 private PluginProperties(){ 93 java.io.InputStream inStream = null; 94 try { 95 try { 96 propertiesFile = getPropertiesFile(); 97 if (null != propertiesFile){ 98 inStream = propertiesFile.getInputStream(); 99 } 100 } catch (java.io.FileNotFoundException fnfe) { 101 Constants.pluginLogger.info(NbBundle.getMessage(PluginProperties.class, "INFO_NO_PROPERTY_FILE")); } catch (java.io.IOException ioe) { 103 Constants.pluginLogger.info(NbBundle.getMessage(PluginProperties.class, "ERR_READING_PROPERTIES")); Constants.pluginLogger.throwing(PluginProperties.class.getName(), "<init>", ioe); 106 } finally { 107 Properties inProps = new Properties (); 108 if (null != inStream){ 109 inProps.load(inStream); 110 inStream.close(); 111 } 112 loadPluginProperties(inProps); 113 } 114 } catch (java.io.IOException ioe) { 115 ErrorManager.getDefault().notify(ErrorManager.WARNING,ioe); 116 } 117 118 } 119 120 private FileObject getPropertiesFile() throws java.io.IOException { 121 FileSystem fs = Repository.getDefault().getDefaultFileSystem(); 122 FileObject dir = fs.findResource("J2EE"); 123 FileObject retVal = null; 124 if (null != dir) { 125 retVal = dir.getFileObject("platform","properties"); if (null == retVal) { 127 retVal = dir.createData("platform","properties"); 129 } 130 } 131 return retVal; 132 } 133 134 135 void loadPluginProperties(Properties inProps) { 136 137 logLevel = inProps.getProperty(LOG_LEVEL_KEY, java.util.logging.Level.OFF.toString()); 138 String [] inputUsers = getArrayPropertyValue(inProps, PRINCIPAL_PREFIX); 139 String [] inputGroups = getArrayPropertyValue(inProps, GROUP_PREFIX); 140 141 setCharsetDisplayPreferenceStatic(Integer.valueOf(inProps.getProperty(CHARSET_DISP_PREF_KEY, "1"))); 142 String b= inProps.getProperty(INCREMENTAL,"true"); incrementalDeployPossible = b.equals("true"); 144 String version = inProps.getProperty(PLUGIN_PROPERTIES_VERSION); boolean needToRegisterDefaultServer = false; 146 147 if ((version==null)||(version!=PLUGIN_CURRENT_VERSION)){ needToRegisterDefaultServer = true; 149 } 150 151 if (needToRegisterDefaultServer){ 152 final File platformRoot = new File (getDefaultInstallRoot()); 153 154 if (isGoodAppServerLocation(platformRoot)){ 155 registerDefaultDomain(platformRoot); 156 saveProperties(); 157 } 158 159 } 160 161 162 163 setUserListStatic(inputUsers); 166 setGroupListStatic(inputGroups); 167 Constants.pluginLogger.setLevel(Level.ALL); 168 jsr88Logger.setLevel(Level.ALL); 169 Level parsed = null; 170 try { 171 parsed = Level.parse(logLevel); 172 jsr88Logger.setLevel(parsed); 173 Constants.pluginLogger.setLevel(parsed); 174 Constants.pluginLogger.log(parsed, logLevel); 175 jsr88Logger.log(parsed, logLevel); 176 } catch (IllegalArgumentException iae) { 177 logLevel = Level.ALL.toString(); 178 Constants.pluginLogger.severe(NbBundle.getMessage(PluginProperties.class, "ERR_ILLEGAL_LEVEL_VALUE")); } 180 181 } 182 183 static public void configureDefaultServerInstance(){ 184 PluginProperties.getDefault(); 186 } 187 188 public void setIncrementalDeploy(Boolean b){ 189 190 incrementalDeployPossible = b.booleanValue(); 191 saveProperties(); 192 193 } 194 public Boolean getIncrementalDeploy(){ 195 return Boolean.valueOf(incrementalDeployPossible); 196 197 } 198 199 public boolean isIncrementalDeploy(){ 200 return incrementalDeployPossible; 201 202 } 203 204 205 209 public String [] getUserList() { 210 SecurityMasterListModel pModel = 211 SecurityMasterListModel.getPrincipalMasterModel(); 212 String [] ss=masterListToStringArray(pModel); 213 return ss; 214 } 216 217 public String [] getGroupList() { 218 SecurityMasterListModel pModel = 219 SecurityMasterListModel.getGroupMasterModel(); 220 221 return masterListToStringArray(pModel); 222 } 224 225 private String [] masterListToStringArray(SecurityMasterListModel pModel) { 226 int len = pModel.getRowCount(); 227 String retVal[] = new String [len]; 228 for (int i = 0; i < len; i++) { 229 String foo = pModel.getRow(i).toString(); 230 retVal[i] = foo; 231 } 232 return retVal; 233 } 234 235 public boolean setUserListStatic(String [] list) { 236 SecurityMasterListModel pModel = 237 SecurityMasterListModel.getPrincipalMasterModel(); 238 boolean retVal = false; 239 if (!containsSameElements(pModel, list)) { 240 241 fillMasterList(pModel, list); 242 retVal = true; 243 } 244 return retVal; 245 } 246 247 public void setUserList(String [] list) { 248 if (setUserListStatic(list)) { 249 saveProperties(); 250 } 251 } 252 253 public boolean setGroupListStatic(String [] list) { 254 SecurityMasterListModel pModel = 255 SecurityMasterListModel.getGroupMasterModel(); 256 boolean retVal = false; 257 if (!containsSameElements(pModel, list)) { 258 fillMasterList(pModel, list); 259 retVal = true; 260 } 261 return retVal; 262 } 263 264 public void setGroupList(String [] list) { 265 if (setGroupListStatic(list)) { 266 saveProperties(); 267 } 268 } 269 270 271 boolean containsSameElements(SecurityMasterListModel pModel, String [] list) { 272 int len = pModel.getRowCount(); 273 if (len != list.length) 274 return false; 275 else 276 for (int i = 0; i < len; i++) { 277 if (!pModel.contains(list[i])) { 278 return false; 279 } 280 } 281 return true; 282 } 283 284 285 286 private void fillMasterList(SecurityMasterListModel pModel, String [] values) { 287 int len = values.length; 288 for (int i = pModel.getRowCount() - 1; i >= 0; i--) { 289 pModel.removeElementAt(i); 290 } 291 for (int i = 0; i < len; i++) { 292 if (!pModel.contains(values[i])) { 293 pModel.addElement(values[i]); 294 } 295 } 296 } 297 298 public String getLogLevel() { 299 return logLevel; 300 } 301 302 public void setLogLevel(String ll) { 303 String oll = logLevel; 304 if (!oll.equals(ll)) { 305 Level parsed = null; 306 try { 307 parsed = Level.parse(ll); 308 jsr88Logger.setLevel(parsed); 309 Constants.pluginLogger.setLevel(parsed); 310 Constants.pluginLogger.log(parsed, ll); 311 jsr88Logger.log(parsed, ll); 312 313 logLevel = ll; 314 saveProperties(); 315 } catch (IllegalArgumentException iae) { 316 Constants.pluginLogger.severe( 318 NbBundle.getMessage(PluginProperties.class, "ERR_ILLEGAL_LEVEL_VALUE")); } 320 } 321 } 322 323 324 325 public Integer getCharsetDisplayPreferenceStatic() { 326 return CharsetMapping.getDisplayOption(); 327 } 328 329 public void setCharsetDisplayPreferenceStatic(Integer displayPreference) { 330 CharsetMapping.setDisplayOption(displayPreference); 331 } 332 333 337 public Integer getCharsetDisplayPreference() { 338 return getCharsetDisplayPreferenceStatic(); 339 } 340 341 345 public void setCharsetDisplayPreference(Integer displayPreference) { 346 Integer oldDisplayPreference = getCharsetDisplayPreferenceStatic(); 347 if(!displayPreference.equals(oldDisplayPreference)) { 348 setCharsetDisplayPreferenceStatic(displayPreference); 349 saveProperties(); 350 } 351 } 352 353 354 private void saveProperties(){ 355 Properties outProp = new Properties (); 356 357 outProp.setProperty(PLUGIN_PROPERTIES_VERSION, PLUGIN_CURRENT_VERSION); 359 360 setArrayPropertyValue(outProp, PRINCIPAL_PREFIX, getUserList()); 361 362 setArrayPropertyValue(outProp, GROUP_PREFIX, getGroupList()); 363 outProp.setProperty(INCREMENTAL, ""+incrementalDeployPossible); 364 365 if (!logLevel.equals(Level.OFF.toString())){ 366 outProp.setProperty(LOG_LEVEL_KEY, logLevel); 367 } 368 if (!getCharsetDisplayPreferenceStatic().equals(CharsetDisplayPreferenceEditor.DEFAULT_PREF_VAL)){ 369 outProp.setProperty(CHARSET_DISP_PREF_KEY, getCharsetDisplayPreferenceStatic().toString()); 370 } 371 372 FileLock l = null; 373 java.io.OutputStream outStream = null; 374 try { 375 if (null != propertiesFile) { 376 try { 377 l = propertiesFile.lock(); 378 outStream = propertiesFile.getOutputStream(l); 379 if (null != outStream){ 380 outProp.store(outStream, ""); 381 } 382 } catch (java.io.IOException ioe) { 383 Constants.pluginLogger.severe( 384 NbBundle.getMessage(PluginProperties.class, "ERR_SAVING_PROPERTIES") ); 386 Constants.pluginLogger.throwing(PluginProperties.class.toString(), "saveChange", ioe); 388 } finally { 389 if (null != outStream){ 390 outStream.close(); 391 } 392 if (null != l){ 393 l.releaseLock(); 394 } 395 } 396 } 397 } catch (java.io.IOException ioe) { 398 Constants.pluginLogger.throwing(PluginProperties.class.toString(), "saveChange",ioe); 399 } 400 401 402 } 403 404 405 406 407 408 public static String getDefaultInstallRoot() { 409 String candidate = System.getProperty(INSTALL_ROOT_PROP_NAME); if (null != candidate){ 411 412 File f = new File (candidate); 413 if (f.exists()){ 414 return candidate; 415 } 416 417 } 418 419 File ff = new File (System.getProperty("netbeans.home")); 420 421 File f3 = new File (ff.getParentFile(),COBUNDLE_DEFAULT_INSTALL_PATH); 422 if ((f3!=null)&&(f3.exists())){ 423 return f3.getAbsolutePath(); 424 } 425 f3 = new File (ff.getParentFile(),COBUNDLE_DEFAULT_INSTALL_PATH2); 426 if ((f3!=null)&&(f3.exists())){ 427 return f3.getAbsolutePath(); 428 } 429 430 431 return ""; 432 } 433 434 435 436 static boolean hasRequiredChildren(File candidate, Collection requiredChildren) { 437 if (null == candidate) { 438 return false; 439 } 440 String [] children = candidate.list(); 441 if (null == children) { 442 return false; 443 } 444 if (null == requiredChildren) { 445 return true; 446 } 447 java.util.List kidsList = java.util.Arrays.asList(children); 448 return kidsList.containsAll(requiredChildren); 449 } 450 451 private static Collection fileColl = new java.util.ArrayList (); 452 453 static { 454 fileColl.add("bin"); fileColl.add("lib"); fileColl.add("config"); } 458 459 460 461 private static boolean isGoodAppServerLocation(File candidate){ 462 if (null == candidate || !candidate.exists() || !candidate.canRead() || 463 !candidate.isDirectory() || !hasRequiredChildren(candidate, fileColl)) { 464 465 return false; 466 } 467 if(ServerLocationManager.isGlassFish(candidate)){ 469 return true; } 471 472 return true; 476 } 477 478 479 private static void registerDefaultDomain(File platformRoot){ 480 String username ="admin"; String password ="adminadmin"; 483 File f = new File (System.getProperty("user.home")+"/.asadminprefs"); FileInputStream fis = null; 486 if (f.exists()) { 487 try{ 488 489 Properties p = new Properties (); 490 fis = new FileInputStream (f); 491 p.load(fis); 492 493 Enumeration e = p.propertyNames() ; 494 for ( ; e.hasMoreElements() ;) { 495 String v = (String )e.nextElement(); 496 if (v.equals("AS_ADMIN_USER")) username = p.getProperty(v ); 498 else if (v.equals("AS_ADMIN_PASSWORD")){ password = p.getProperty(v ); 500 } 501 } 502 503 } catch (Exception e){ 504 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL,e); 506 } finally { 507 if (null != fis) { 508 try { 509 fis.close(); 510 } catch (IOException e) { 511 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL,e); 512 } 513 } 514 } 515 516 } 517 String ext = (File.separatorChar == '/' ? "conf" : "bat"); File asenv = new File (platformRoot,"config/asenv."+ext); Asenv asenvContent = new Asenv(asenv); 521 String defDomainsDirName = asenvContent.get(Asenv.AS_DEF_DOMAINS_PATH); 522 File domains = new File (defDomainsDirName); if (domains.exists() && domains.isDirectory() ) { 524 File [] domainsList= domains.listFiles(); 525 if(domainsList==null) { 526 return; 527 } 528 for (int i=0;i<domainsList.length;i++){ 529 530 try { 531 532 File confDir = new File (domainsList[i].getAbsolutePath()+"/config"); if (confDir.exists() && confDir.isDirectory() && confDir.canWrite()) { 535 String hp = Util.getHostPort(domainsList[i],platformRoot); 537 if (hp!=null){ 538 539 540 String dmUrl = "["+platformRoot.getAbsolutePath()+"]" +SunURIManager.SUNSERVERSURI+hp; String displayName = NbBundle.getMessage(PluginProperties.class, "OpenIDE-Module-Name") ; if (ServerLocationManager.isGlassFish(platformRoot)){ 543 displayName+=" 9"; } 545 if (i!=0) { displayName = domainsList[i].getName(); 547 } 548 Repository rep = (Repository)Lookup.getDefault().lookup(Repository.class); 549 FileObject dir = rep.getDefaultFileSystem().findResource("/J2EE/InstalledServers"); FileObject instanceFOs[] = dir.getChildren(); 551 FileObject instanceFO = null; 552 for (int j = 0; j < instanceFOs.length; j++) { 553 if (dmUrl.equals(instanceFOs[j].getAttribute(InstanceProperties.URL_ATTR))) { 554 instanceFO = instanceFOs[j]; 555 } 556 } 557 if (instanceFO == null) { 558 String name = FileUtil.findFreeFileName(dir, "instance", null); instanceFO = dir.createData(name); 560 } 561 instanceFO.setAttribute(InstanceProperties.URL_ATTR, dmUrl); 562 instanceFO.setAttribute(InstanceProperties.USERNAME_ATTR, username); 563 instanceFO.setAttribute(InstanceProperties.PASSWORD_ATTR, password); 564 instanceFO.setAttribute(InstanceProperties.DISPLAY_NAME_ATTR, displayName); 565 instanceFO.setAttribute("DOMAIN", domainsList[i].getName()); instanceFO.setAttribute("LOCATION", asenvContent.get(Asenv.AS_DEF_DOMAINS_PATH)); } 569 } 570 } catch (IOException ioe){ 571 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ioe); 572 } 574 } 575 } 576 577 578 } 579 580 593 private static String [] getArrayPropertyValue(Properties inProps, String prefix) { 594 String prototype[] = new String [0]; 595 java.util.List l = new java.util.ArrayList (); 596 int index = 0; 597 String entry = null; 598 do { 599 entry = inProps.getProperty(prefix+index); 600 index++; 601 if (null != entry) { 602 l.add(entry); 603 } 604 } 605 while (null != entry); 606 Object [] retVal = l.toArray(prototype); 607 return (String []) retVal; 608 } 609 610 619 private static void setArrayPropertyValue(Properties props, String prefix, String [] values) { 620 int len = 0; 621 if (null != values) { 622 len = values.length; 623 } 624 int index = 0; 625 for (int i = 0; i < len; i++) { 626 if (null != values[i]) { 627 props.setProperty(prefix+index,values[i]); 628 index++; 629 } 630 } 631 }} 632 | Popular Tags |