001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Sam
028: */
029:
030: package com.caucho.netbeans;
031:
032: import com.caucho.netbeans.PluginL10N;
033: import com.caucho.netbeans.PluginLogger;
034:
035: import org.netbeans.api.java.platform.JavaPlatform;
036: import org.netbeans.api.java.platform.JavaPlatformManager;
037: import org.netbeans.api.java.platform.Specification;
038: import org.netbeans.modules.j2ee.deployment.plugins.api.InstanceCreationException;
039: import org.netbeans.modules.j2ee.deployment.plugins.api.InstanceProperties;
040: import org.openide.filesystems.FileObject;
041: import org.openide.filesystems.FileUtil;
042:
043: import java.io.File;
044: import java.net.InetAddress;
045: import java.net.UnknownHostException;
046: import java.net.URL;
047: import java.util.logging.Level;
048: import java.util.ArrayList;
049: import java.util.List;
050: import java.util.logging.Logger;
051: import javax.enterprise.deploy.spi.exceptions.DeploymentManagerCreationException;
052:
053: public class ResinConfiguration implements Cloneable {
054: private static final PluginL10N L = new PluginL10N(
055: ResinConfiguration.class);
056: private static final Logger log = Logger
057: .getLogger(ResinConfiguration.class.getName());
058:
059: private static final String PROPERTY_AUTOLOAD_ENABLED = "autoload_enabled";
060: private static final String PROPERTY_DEBUG_PORT = "debugger_port";
061: private static final String PROPERTY_DISPLAY_NAME = InstanceProperties.DISPLAY_NAME_ATTR;
062: private static final String PROPERTY_JAVA_PLATFORM = "java_platform";
063: private static final String PROPERTY_JAVA_OPTS = "java_opts";
064:
065: private static final String PLATFORM_PROPERTY_ANT_NAME = "platform.ant.name";
066:
067: private static final String URI_TOKEN_HOME = ":home=";
068: private static final String URI_TOKEN_CONF = ":conf=";
069: private static final String URI_TOKEN_SERVER_ID = ":server-id=";
070: private static final String URI_TOKEN_SERVER_PORT = ":server-port=";
071: private static final String URI_TOKEN_SERVER_ADDRESS = ":server-address=";
072:
073: private File _resinHome;
074: private File _resinConf;
075: private String _serverId;
076: private String _serverAddress;
077: private int _serverPort;
078: private JavaPlatform _javaPlatform;
079: private int _debugPort = 0;
080: private int _startTimeout = 60 * 1000;
081: private int _stopTimeout = 60 * 1000;
082:
083: private String _displayName = "Resin";
084: private String _username = "Username";
085: private String _password = "Password";
086:
087: private String _uri;
088: private InstanceProperties _ip;
089: private boolean _isInit;
090:
091: public ResinConfiguration() {
092: JavaPlatformManager platformManager = JavaPlatformManager
093: .getDefault();
094: _javaPlatform = platformManager.getDefaultPlatform();
095: }
096:
097: public ResinConfiguration(InstanceProperties ip)
098: throws DeploymentManagerCreationException {
099: _ip = ip;
100:
101: _uri = ip.getProperty(InstanceProperties.URL_ATTR);
102:
103: parseURI(_uri);
104: }
105:
106: String getContextPath() {
107: return "/test";
108: }
109:
110: int getPort() {
111: String port = _ip.getProperty("resin.port");
112:
113: try {
114: return Integer.parseInt(port);
115: } catch (Exception e) {
116: throw new RuntimeException(e);
117: }
118: }
119:
120: private void init() {
121: if (_isInit)
122: return;
123:
124: _isInit = true;
125:
126: setUsername(_ip.getProperty(InstanceProperties.USERNAME_ATTR));
127: setPassword(_ip.getProperty(InstanceProperties.PASSWORD_ATTR));
128: setDisplayName(_ip
129: .getProperty(InstanceProperties.DISPLAY_NAME_ATTR));
130:
131: setJavaPlatformByName(_ip.getProperty(PROPERTY_JAVA_PLATFORM));
132:
133: String resinHome = _ip.getProperty("resin.home");
134:
135: if (resinHome == null)
136: throw new RuntimeException("resin.home is invalid");
137:
138: _resinHome = new File(resinHome);
139: String debugPort = _ip.getProperty(PROPERTY_DEBUG_PORT);
140:
141: if (debugPort != null) {
142: try {
143: setDebugPort(Integer.parseInt(debugPort));
144: } catch (NumberFormatException e) {
145: // no-op
146: }
147: }
148:
149: String port = _ip.getProperty("resin.port");
150: if (port != null) {
151: try {
152: _serverPort = Integer.parseInt(port);
153: } catch (NumberFormatException e) {
154: throw new RuntimeException(e);
155: }
156: }
157: }
158:
159: @Override
160: protected Object clone() {
161: try {
162: return super .clone();
163: } catch (CloneNotSupportedException e) {
164: throw new AssertionError(e);
165: }
166: }
167:
168: public InstanceProperties toInstanceProperties()
169: throws InstanceCreationException {
170: String username = _username;
171: String password = _password;
172: String displayName = _displayName;
173:
174: if (username == null)
175: username = "resin";
176:
177: if (password == null)
178: password = "resin";
179:
180: if (displayName == null)
181: displayName = "Resin";
182:
183: InstanceProperties instanceProperties = InstanceProperties
184: .createInstanceProperties(getURI(), username, password,
185: displayName);
186:
187: if (_debugPort > 0)
188: instanceProperties.setProperty(PROPERTY_DEBUG_PORT, String
189: .valueOf(_debugPort));
190:
191: if (_javaPlatform != null)
192: instanceProperties.setProperty(PROPERTY_JAVA_PLATFORM,
193: getJavaPlatformName(_javaPlatform));
194:
195: return instanceProperties;
196: }
197:
198: public File getResinConf() {
199: init();
200:
201: return _resinConf;
202: }
203:
204: public void setResinConf(File resinConf) {
205: if (!resinConf.isAbsolute()) {
206: if (_resinHome == null)
207: throw new IllegalArgumentException(L.l(
208: "no resin.home set for relative conf {0}",
209: resinConf));
210:
211: resinConf = new File(_resinHome, resinConf.getPath());
212: }
213:
214: _resinConf = resinConf;
215: }
216:
217: public File getResinHome() {
218: init();
219:
220: return _resinHome;
221: }
222:
223: public void setResinHome(File resinHome) {
224: _resinHome = resinHome;
225: }
226:
227: public String getServerId() {
228: return _serverId;
229: }
230:
231: public void setServerId(String serverId) {
232: _serverId = serverId;
233: }
234:
235: public String getServerAddress() {
236: return _serverAddress == null ? "127.0.0.1" : _serverAddress;
237: }
238:
239: private void setServerAddress(String serverAddress)
240: throws IllegalArgumentException
241:
242: {
243: try {
244: InetAddress.getByName(serverAddress);
245:
246: _serverAddress = serverAddress;
247: } catch (UnknownHostException e) {
248: throw new IllegalArgumentException(L.l(
249: "The address ''{0}'' is not valid: {1}",
250: serverAddress, e.getLocalizedMessage()));
251: }
252: }
253:
254: public int getServerPort() {
255: return _serverPort;
256: }
257:
258: public void setServerPort(int serverPort) {
259: _serverPort = serverPort;
260: }
261:
262: public void setServerPort(String serverPort)
263: throws IllegalArgumentException {
264: int port = 0;
265:
266: try {
267: port = Integer.parseInt(serverPort);
268: } catch (NumberFormatException ex) {
269: // no-op
270: }
271:
272: if (!(0 < port && port < 65536))
273: throw new IllegalArgumentException(
274: L
275: .l("server-port must have a value between 0 and 65536"));
276:
277: setServerPort(port);
278: }
279:
280: public String getDisplayName() {
281: return _displayName;
282: }
283:
284: public void setDisplayName(String displayName) {
285: _displayName = displayName;
286: }
287:
288: public String getPassword() {
289: return _password;
290: }
291:
292: public void setPassword(String password) {
293: _password = password;
294: }
295:
296: public String getUsername() {
297: return _username;
298: }
299:
300: public void setUsername(String username) {
301: _username = username;
302: }
303:
304: /**
305: * Returns the debug port, 0 means a free port should be determnined.
306: */
307: public int getDebugPort() {
308: return _debugPort;
309: }
310:
311: public void setDebugPort(int debugPort) {
312: _debugPort = debugPort;
313: }
314:
315: /**
316: * Returns the java platform.
317: */
318: public JavaPlatform getJavaPlatform() {
319: return _javaPlatform;
320: }
321:
322: public void setJavaPlatform(JavaPlatform javaPlatform) {
323: _javaPlatform = javaPlatform;
324: }
325:
326: public static String getJavaPlatformName(JavaPlatform javaPlatform) {
327: return ((String) javaPlatform.getProperties().get(
328: PLATFORM_PROPERTY_ANT_NAME));
329: }
330:
331: public void setJavaPlatformByName(String javaPlatformName) {
332: JavaPlatformManager platformManager = JavaPlatformManager
333: .getDefault();
334: JavaPlatform javaPlatform = platformManager
335: .getDefaultPlatform();
336:
337: JavaPlatform[] installedPlatforms = platformManager
338: .getPlatforms(null, new Specification("J2SE", null));
339:
340: for (JavaPlatform installedPlatform : installedPlatforms) {
341: String platformName = getJavaPlatformName(installedPlatform);
342:
343: if (platformName != null
344: && platformName.equals(javaPlatformName)) {
345: javaPlatform = installedPlatform;
346: break;
347: }
348: }
349:
350: _javaPlatform = javaPlatform;
351: }
352:
353: public int getStartTimeout() {
354: return _startTimeout;
355: }
356:
357: public void setStartTimeout(int startTimeout) {
358: _startTimeout = startTimeout;
359: }
360:
361: public int getStopTimeout() {
362: return _stopTimeout;
363: }
364:
365: public void setStopTimeout(int stopTimeout) {
366: _stopTimeout = stopTimeout;
367: }
368:
369: /**
370: * Calculates a javaHome based on the {@link #getJavaPlatform()}
371: * javaHome.
372: */
373: public File calculateJavaHome() {
374: JavaPlatform javaPlatform = _javaPlatform;
375:
376: if (javaPlatform == null)
377: javaPlatform = JavaPlatformManager.getDefault()
378: .getDefaultPlatform();
379:
380: return FileUtil.toFile((FileObject) javaPlatform
381: .getInstallFolders().iterator().next());
382: }
383:
384: public List<URL> getClasses() {
385: // XXX: s/b urls to Resin libraries
386: return new ArrayList<URL>();
387: }
388:
389: public List<URL> getSources() {
390: // XXX: s/b urls to Resin sources
391: return new ArrayList<URL>();
392: }
393:
394: public List<URL> getJavadocs() {
395: return new ArrayList<URL>();
396: }
397:
398: private void requiredFile(String name, File file)
399: throws IllegalStateException {
400: if (file == null)
401: throw new IllegalStateException(L.l("''{0}'' is required",
402: name));
403:
404: if (!file.exists())
405: throw new IllegalStateException(L.l(
406: "''{0}'' does not exist", file));
407: }
408:
409: public void validate() throws IllegalStateException {
410: log.info("validate");
411: requiredFile("resin.home", getResinHome());
412: //requiredFile("resin-conf", _resinConf);
413: /*
414: try {
415: InetAddress.getByName(getServerAddress());
416: }
417: catch (UnknownHostException e) {
418: throw new IllegalStateException(L.l("server-address ''{0}'' is not valid: {1}",
419: getServerAddress(),
420: e.getLocalizedMessage()));
421: }
422: */
423: if (!(0 < _serverPort && _serverPort < 65536))
424: throw new IllegalStateException(
425: L
426: .l("''server-port'' must have a value between 0 and 65536"));
427:
428: }
429:
430: void parseURI(String uri) throws IllegalArgumentException {
431: if (!uri.startsWith("resin"))
432: throw new IllegalArgumentException(L.l(
433: "''{0}'' is not a Resin URI", uri));
434:
435: String token = null;
436: int i = "resin".length();
437: int lexemeStart = i;
438:
439: try {
440: while (true) {
441: String nextToken = parseURIToken(uri, i);
442:
443: if (nextToken != null || i == uri.length()) {
444: if (token != null && i > lexemeStart) {
445: String lexeme = uri.substring(lexemeStart, i);
446:
447: if (token == URI_TOKEN_HOME)
448: setResinHome(new File(lexeme));
449: else if (token == URI_TOKEN_CONF)
450: setResinConf(new File(lexeme));
451: else if (token == URI_TOKEN_SERVER_ID)
452: setServerId(lexeme);
453: else if (token == URI_TOKEN_SERVER_PORT)
454: setServerPort(lexeme);
455: else if (token == URI_TOKEN_SERVER_ADDRESS)
456: setServerAddress(lexeme);
457: else
458: throw new AssertionError(token);
459: }
460:
461: if (i == uri.length())
462: break;
463:
464: token = nextToken;
465: i += token.length();
466: lexemeStart = i;
467: } else
468: i++;
469: }
470: } catch (Exception ex) {
471: log.log(Level.FINER, ex.toString(), ex);
472:
473: throw new IllegalArgumentException(L.l(
474: "problem parsing URI ''{0}'': {1}", uri, ex));
475: }
476: }
477:
478: private String parseURIToken(String uri, int i) {
479: if (uri.regionMatches(i, URI_TOKEN_HOME, 0, URI_TOKEN_HOME
480: .length()))
481: return URI_TOKEN_HOME;
482: if (uri.regionMatches(i, URI_TOKEN_CONF, 0, URI_TOKEN_CONF
483: .length()))
484: return URI_TOKEN_CONF;
485: if (uri.regionMatches(i, URI_TOKEN_SERVER_ID, 0,
486: URI_TOKEN_SERVER_ID.length()))
487: return URI_TOKEN_SERVER_ID;
488: if (uri.regionMatches(i, URI_TOKEN_SERVER_PORT, 0,
489: URI_TOKEN_SERVER_PORT.length()))
490: return URI_TOKEN_SERVER_PORT;
491: if (uri.regionMatches(i, URI_TOKEN_SERVER_ADDRESS, 0,
492: URI_TOKEN_SERVER_ADDRESS.length()))
493: return URI_TOKEN_SERVER_ADDRESS;
494: else
495: return null;
496: }
497:
498: public String getURI() {
499: StringBuilder uri = new StringBuilder();
500:
501: uri.append("resin");
502:
503: if (_resinHome != null) {
504: uri.append(URI_TOKEN_HOME);
505: uri.append(_resinHome.getAbsolutePath());
506: }
507:
508: if (_resinConf != null) {
509: uri.append(":conf=");
510: uri.append(_resinConf.getAbsolutePath());
511: }
512:
513: if (_serverId != null) {
514: uri.append(URI_TOKEN_SERVER_ID);
515: uri.append(_serverId);
516: }
517:
518: if ((0 < _serverPort && _serverPort < 65536)) {
519: uri.append(URI_TOKEN_SERVER_PORT);
520: uri.append(_serverPort);
521: }
522:
523: if (_serverAddress != null) {
524: uri.append(URI_TOKEN_SERVER_ADDRESS);
525: uri.append(_serverAddress);
526: }
527:
528: return uri.toString();
529: }
530:
531: public String toString() {
532: return getClass().getSimpleName() + "[" + getURI() + "]";
533: }
534:
535: }
|