1 19 package org.netbeans.modules.j2ee.jboss4.util; 20 21 import java.io.BufferedInputStream ; 22 import java.io.File ; 23 import java.io.FileInputStream ; 24 import java.io.FileNotFoundException ; 25 import java.io.FilenameFilter ; 26 import java.io.IOException ; 27 import java.io.InputStream ; 28 import java.net.MalformedURLException ; 29 import java.net.URL ; 30 import java.util.ArrayList ; 31 import java.util.Enumeration ; 32 import java.util.List ; 33 import java.util.Properties ; 34 import java.util.logging.Level ; 35 import java.util.logging.Logger ; 36 import org.netbeans.api.java.platform.JavaPlatform; 37 import org.netbeans.api.java.platform.JavaPlatformManager; 38 import org.netbeans.api.java.platform.Specification; 39 import org.netbeans.modules.j2ee.deployment.plugins.api.InstanceProperties; 40 import org.netbeans.modules.j2ee.jboss4.JBDeploymentManager; 41 import org.netbeans.modules.j2ee.jboss4.customizer.CustomizerSupport; 42 import org.openide.ErrorManager; 43 import org.openide.filesystems.FileUtil; 44 import org.openide.modules.InstalledFileLocator; 45 import org.openide.util.NbCollections; 46 47 52 public class JBProperties { 53 54 55 public static final String PLAT_PROP_ANT_NAME = "platform.ant.name"; 57 public static final String PROP_PROXY_ENABLED = "proxy_enabled"; private static final String PROP_JAVA_PLATFORM = "java_platform"; private static final String PROP_JAVA_OPTS = "java_opts"; private static final String PROP_SOURCES = "sources"; private static final String PROP_JAVADOCS = "javadocs"; private static final String PROP_SERVER_DIR = "server-dir"; private static final String PROP_ROOT_DIR = "root-dir"; 66 private static final String DEF_VALUE_JAVA_OPTS = ""; private static final boolean DEF_VALUE_PROXY_ENABLED = true; 69 70 private final InstanceProperties ip; 71 private final JBDeploymentManager manager; 72 73 private String username = "admin"; private String password = "admin"; 77 78 private long updateCredentialsTimestamp; 79 80 private static final Logger LOGGER = Logger.getLogger(JBProperties.class.getName()); 81 82 83 84 public JBProperties(JBDeploymentManager manager) { 85 this.manager = manager; 86 ip = manager.getInstanceProperties(); 87 } 88 89 public boolean supportsJavaEE5ejb3() { 90 return new File (getServerDir(), "deploy/ejb3.deployer").exists() || new File (getServerDir(), "deployers/ejb3.deployer").exists(); } 93 94 public boolean supportsJavaEE5web() { 95 return new File (getServerDir(), "deployers/jbossweb.deployer").exists(); } 97 98 public File getServerDir() { 99 return new File (ip.getProperty(PROP_SERVER_DIR)); 100 } 101 102 public File getRootDir() { 103 return new File (ip.getProperty(PROP_ROOT_DIR)); 104 } 105 106 public boolean getProxyEnabled() { 107 String val = ip.getProperty(PROP_PROXY_ENABLED); 108 return val != null ? Boolean.valueOf(val).booleanValue() 109 : DEF_VALUE_PROXY_ENABLED; 110 } 111 112 public void setProxyEnabled(boolean enabled) { 113 ip.setProperty(PROP_PROXY_ENABLED, Boolean.toString(enabled)); 114 } 115 116 public JavaPlatform getJavaPlatform() { 117 String currentJvm = ip.getProperty(PROP_JAVA_PLATFORM); 118 JavaPlatformManager jpm = JavaPlatformManager.getDefault(); 119 JavaPlatform[] installedPlatforms = jpm.getPlatforms(null, new Specification("J2SE", null)); for (int i = 0; i < installedPlatforms.length; i++) { 121 String platformName = (String )installedPlatforms[i].getProperties().get(PLAT_PROP_ANT_NAME); 122 if (platformName != null && platformName.equals(currentJvm)) { 123 return installedPlatforms[i]; 124 } 125 } 126 return jpm.getDefaultPlatform(); 128 } 129 130 public void setJavaPlatform(JavaPlatform javaPlatform) { 131 ip.setProperty(PROP_JAVA_PLATFORM, (String )javaPlatform.getProperties().get(PLAT_PROP_ANT_NAME)); 132 } 133 134 public String getJavaOpts() { 135 String val = ip.getProperty(PROP_JAVA_OPTS); 136 return val != null ? val 137 : DEF_VALUE_JAVA_OPTS; 138 } 139 140 public void setJavaOpts(String javaOpts) { 141 ip.setProperty(PROP_JAVA_OPTS, javaOpts); 142 } 143 144 public List <URL > getClasses() { 145 List <URL > list = new ArrayList <URL >(); 146 try { 147 File rootDir = getRootDir(); 148 File serverDir = getServerDir(); 149 list.add(fileToUrl(new File (rootDir, "client/jboss-j2ee.jar"))); 151 File wsClientLib = new File (rootDir, "client/jbossws-client.jar"); if (wsClientLib.exists()) { 153 list.add(fileToUrl(wsClientLib)); 154 } 155 156 addFiles(new File (rootDir, "lib"), list); addFiles(new File (serverDir, "/lib"), list); if (supportsJavaEE5ejb3()) { 159 File ejb3deployer = new File (serverDir, "/deploy/ejb3.deployer/"); if (ejb3deployer.exists()) { 161 addFiles(ejb3deployer, list); 162 } 163 else 164 if ((ejb3deployer = new File (serverDir, "/deployers/ejb3.deployer/")).exists()) { addFiles(ejb3deployer, list); 166 } 167 } 168 169 File jsfAPI = new File (serverDir, "/deploy/jbossweb-tomcat55.sar/jsf-libs/myfaces-api.jar"); if (jsfAPI.exists()) { 171 try { 172 list.add(fileToUrl(jsfAPI)); 173 } catch (MalformedURLException e) { 174 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 175 } 176 } 177 else 178 if ((jsfAPI = new File (serverDir, "/deployers/jbossweb.deployer/jsf-libs/jsf-api.jar")).exists()) { try { 180 list.add(fileToUrl(jsfAPI)); 181 } catch (MalformedURLException e) { 182 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 183 } 184 } 185 186 File jsfIMPL = new File (serverDir, "/deploy/jbossweb-tomcat55.sar/jsf-libs/myfaces-impl.jar"); if (jsfIMPL.exists()) { 188 try { 189 list.add(fileToUrl(jsfIMPL)); 190 } catch (MalformedURLException e) { 191 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 192 } 193 } 194 else 195 if ((jsfIMPL = new File (serverDir, "/deployers/jbossweb.deployer/jsf-libs/jsf-impl.jar")).exists()) { try { 197 list.add(fileToUrl(jsfIMPL)); 198 } catch (MalformedURLException e) { 199 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 200 } 201 } 202 } catch (MalformedURLException e) { 203 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 204 } 205 return list; 206 } 207 208 private static class FF implements FilenameFilter { 209 public boolean accept(File dir, String name) { 210 return name.endsWith(".jar") || new File (dir, name).isDirectory(); } 212 } 213 214 private void addFiles(File folder, List l) { 215 File files [] = folder.listFiles(new FF()); 216 if (files == null) 217 return; 218 for (int i = 0; i < files.length; i++) { 219 if (files [i].isDirectory()) { 220 addFiles(files [i], l); 221 } else { 222 try { 223 l.add(fileToUrl(files [i])); 224 } catch (MalformedURLException e) { 225 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 226 } 227 } 228 } 229 } 230 231 public List <URL > getSources() { 232 String path = ip.getProperty(PROP_SOURCES); 233 if (path == null) { 234 return new ArrayList <URL >(); 235 } 236 return CustomizerSupport.tokenizePath(path); 237 } 238 239 public void setSources(List <URL > path) { 240 ip.setProperty(PROP_SOURCES, CustomizerSupport.buildPath(path)); 241 manager.getJBPlatform().notifyLibrariesChanged(); 242 } 243 244 public List <URL > getJavadocs() { 245 String path = ip.getProperty(PROP_JAVADOCS); 246 if (path == null) { 247 ArrayList <URL > list = new ArrayList <URL >(); 248 try { 249 File j2eeDoc = InstalledFileLocator.getDefault().locate("docs/javaee5-doc-api.zip", null, false); if (j2eeDoc != null) { 251 list.add(fileToUrl(j2eeDoc)); 252 } 253 } catch (MalformedURLException e) { 254 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 255 } 256 return list; 257 } 258 return CustomizerSupport.tokenizePath(path); 259 } 260 261 public void setJavadocs(List <URL > path) { 262 ip.setProperty(PROP_JAVADOCS, CustomizerSupport.buildPath(path)); 263 manager.getJBPlatform().notifyLibrariesChanged(); 264 } 265 266 public synchronized String getUsername() { 267 updateCredentials(); 268 return username; 269 } 270 271 public synchronized String getPassword() { 272 updateCredentials(); 273 return password; 274 } 275 276 278 private synchronized void updateCredentials() { 279 File usersPropFile = new File (getServerDir(), "/conf/props/jmx-console-users.properties"); 280 long lastModified = usersPropFile.lastModified(); 281 if (lastModified == updateCredentialsTimestamp) { 282 LOGGER.log(Level.FINER, "Credentials are up-to-date."); 283 return; 284 } 285 Properties usersProps = new Properties (); 286 try { 287 InputStream is = new BufferedInputStream (new FileInputStream (usersPropFile)); 288 try { 289 usersProps.load(is); 290 } finally { 291 is.close(); 292 } 293 } catch (FileNotFoundException e) { 294 LOGGER.log(Level.WARNING, usersPropFile + " not found.", e); 295 return; 296 } catch (IOException e) { 297 LOGGER.log(Level.WARNING, "Error while reading " + usersPropFile, e); 298 return; 299 } 300 301 Enumeration <String > names = NbCollections.checkedEnumerationByFilter(usersProps.propertyNames(), String .class, false); 302 if (names.hasMoreElements()) { 303 username = names.nextElement(); 304 password = usersProps.getProperty(username); 305 } 306 307 updateCredentialsTimestamp = lastModified; 308 } 309 310 311 private static URL fileToUrl(File file) throws MalformedURLException { 312 URL url = file.toURI().toURL(); 313 if (FileUtil.isArchiveFile(url)) { 314 url = FileUtil.getArchiveRoot(url); 315 } 316 return url; 317 } 318 } 319 | Popular Tags |