001: /*
002: License $Id: Builder.java,v 1.7 2005/02/10 18:25:53 hendriks73 Exp $
003:
004: Copyright (c) 2001-2005 tagtraum industries.
005:
006: LGPL
007: ====
008:
009: jo! is free software; you can redistribute it and/or
010: modify it under the terms of the GNU Lesser General Public
011: License as published by the Free Software Foundation; either
012: version 2.1 of the License, or (at your option) any later version.
013:
014: jo! 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. See the GNU
017: Lesser General Public License for more details.
018:
019: You should have received a copy of the GNU Lesser General Public
020: License along with this library; if not, write to the Free Software
021: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
022:
023: For LGPL see <http://www.fsf.org/copyleft/lesser.txt>
024:
025:
026: Sun license
027: ===========
028:
029: This release contains software by Sun Microsystems. Therefore
030: the following conditions have to be met, too. They apply to the
031: files
032:
033: - lib/mail.jar
034: - lib/activation.jar
035: - lib/jsse.jar
036: - lib/jcert.jar
037: - lib/jaxp.jar
038: - lib/crimson.jar
039: - lib/servlet.jar
040: - lib/jnet.jar
041: - lib/jaas.jar
042: - lib/jaasmod.jar
043:
044: contained in this release.
045:
046: a. Licensee may not modify the Java Platform
047: Interface (JPI, identified as classes contained within the javax
048: package or any subpackages of the javax package), by creating additional
049: classes within the JPI or otherwise causing the addition to or modification
050: of the classes in the JPI. In the event that Licensee creates any
051: Java-related API and distribute such API to others for applet or
052: application development, you must promptly publish broadly, an accurate
053: specification for such API for free use by all developers of Java-based
054: software.
055:
056: b. Software is confidential copyrighted information of Sun and
057: title to all copies is retained by Sun and/or its licensors. Licensee
058: shall not modify, decompile, disassemble, decrypt, extract, or otherwise
059: reverse engineer Software. Software may not be leased, assigned, or
060: sublicensed, in whole or in part. Software is not designed or intended
061: for use in on-line control of aircraft, air traffic, aircraft navigation
062: or aircraft communications; or in the design, construction, operation or
063: maintenance of any nuclear facility. Licensee warrants that it will not
064: use or redistribute the Software for such purposes.
065:
066: c. Software is provided "AS IS," without a warranty
067: of any kind. ALL EXPRESS OR IMPLIED REPRESENTATIONS AND WARRANTIES,
068: INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
069: PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED.
070:
071: d. This License is effective until terminated. Licensee may
072: terminate this License at any time by destroying all copies of Software.
073: This License will terminate immediately without notice from Sun if Licensee
074: fails to comply with any provision of this License. Upon such termination,
075: Licensee must destroy all copies of Software.
076:
077: e. Software, including technical data, is subject to U.S.
078: export control laws, including the U.S. Export Administration Act and its
079: associated regulations, and may be subject to export or import regulations
080: in other countries. Licensee agrees to comply strictly with all such
081: regulations and acknowledges that it has the responsibility to obtain
082: licenses to export, re-export, or import Software. Software may not be
083: downloaded, or otherwise exported or re-exported (i) into, or to a national
084: or resident of, Cuba, Iraq, Iran, North Korea, Libya, Sudan, Syria or any
085: country to which the U.S. has embargoed goods; or (ii) to anyone on the
086: U.S. Treasury Department's list of Specially Designated Nations or the U.S.
087: Commerce Department's Table of Denial Orders.
088:
089:
090: Feedback
091: ========
092:
093: We encourage your feedback and suggestions and want to use your feedback to
094: improve the Software. Send all such feedback to:
095: <feedback@tagtraum.com>
096:
097: For more information on tagtraum industries and jo!
098: please see <http://www.tagtraum.com/>.
099:
100:
101: */
102: package com.tagtraum.metaserver;
103:
104: import com.tagtraum.framework.log.C_Log;
105: import com.tagtraum.framework.log.Log;
106: import com.tagtraum.framework.log.LogWriter;
107: import com.tagtraum.framework.server.I_TCPListener;
108: import com.tagtraum.framework.server.TCPListener;
109: import com.tagtraum.framework.util.*;
110:
111: import java.io.File;
112: import java.io.FileWriter;
113: import java.io.IOException;
114: import java.net.InetAddress;
115: import java.net.URL;
116: import java.net.UnknownHostException;
117: import java.util.Enumeration;
118: import java.util.Properties;
119: import java.util.StringTokenizer;
120: import java.util.Vector;
121:
122: /**
123: *
124: * @author Hendrik Schreiber
125: * @version 1.1beta1 $Id: Builder.java,v 1.7 2005/02/10 18:25:53 hendriks73 Exp $
126: */
127: public class Builder extends AbstractBuilder {
128:
129: /**
130: * Source-Version
131: */
132: public static String vcid = "$Id: Builder.java,v 1.7 2005/02/10 18:25:53 hendriks73 Exp $";
133:
134: /**
135: * Indicates whether the built object needs to be rebuilt.
136: *
137: * @return true or false
138: */
139: public boolean needsRebuild() {
140: return false;
141: }
142:
143: /**
144: * Rebuilds the object.
145: *
146: * @param obj object to rebuild
147: */
148: public void rebuild(Object obj) throws BuildException {
149: MetaServer theService = (MetaServer) obj;
150:
151: try {
152: Properties theProperties = loadProperties(new URL(getURL(),
153: "metaserver.properties"));
154:
155: buildService(theService, getName(), theProperties, getURL());
156:
157: buildListeners(
158: new URL(getURL(), "metalistener.properties"),
159: theService);
160: } catch (Throwable t) {
161: throw new BuildException(t);
162: }
163: }
164:
165: /**
166: * Builds an object with a specified name.
167: *
168: * @return the object
169: */
170: public Object build() throws BuildException {
171: MetaServer theService = null;
172:
173: try {
174: Properties theProperties = loadProperties(new URL(getURL(),
175: "metaserver.properties"));
176:
177: theService = buildService(null, getName(), theProperties,
178: getURL());
179:
180: buildListeners(
181: new URL(getURL(), "metalistener.properties"),
182: theService);
183: } catch (Throwable t) {
184: throw new BuildException(t);
185: }
186:
187: return theService;
188: }
189:
190: public MetaServer buildService(MetaServer theService, String aName,
191: Properties theProperties, URL aURL) throws IOException,
192: BuildException {
193: try {
194: String theName = theProperties.getProperty(aName + ".name");
195:
196: if (theName == null) {
197: theName = "MetaServer";
198:
199: Log.getLog(theName).log(
200: "Couldn't parse '" + aName
201: + ".name'. Using default value: "
202: + theName);
203: }
204:
205: String theEventLogName = theProperties.getProperty(aName
206: + ".log");
207: URL theLogURL = null;
208:
209: if (theEventLogName == null) {
210: theLogURL = new URL(aURL, "metaserver.log");
211: } else if (!theEventLogName.equalsIgnoreCase("NONE")) {
212: theLogURL = URLHelper.make(aURL, StringHelper
213: .replaceWithSystemProperty(theEventLogName));
214: }
215: if (theLogURL != null) {
216: try {
217: final File logFile = new File(PlatformHelper
218: .getOSSpecificPath(theLogURL.getFile()));
219: logFile.getParentFile().mkdirs();
220: LogWriter theLogWriter = new LogWriter(
221: new FileWriter(logFile, true), 1);
222: Log.getLog(theName).addI_LogEventListener(
223: theLogWriter);
224: } catch (IOException ioe) {
225: Log.getLog(getName()).log(
226: "Failed to create metaserver log: " + ioe,
227: C_Log.ERROR);
228: }
229: }
230:
231: int theMajorVersion = 0;
232:
233: try {
234: theMajorVersion = Integer.parseInt(theProperties
235: .getProperty(aName + ".majorversion"));
236: } catch (Exception e) {
237: Log
238: .getLog(theName)
239: .log(
240: "Failed to parse '"
241: + aName
242: + ".majorversion'. Using default value: "
243: + theMajorVersion);
244: }
245:
246: int theMinorVersion = 8;
247:
248: try {
249: theMinorVersion = Integer.parseInt(theProperties
250: .getProperty(aName + ".minorversion"));
251: } catch (Exception e) {
252: Log
253: .getLog(theName)
254: .log(
255: "Couldn't parse '"
256: + aName
257: + ".minorversion'. Using default value: "
258: + theMinorVersion);
259: }
260:
261: int theMaxHandlerThreads = 1;
262:
263: try {
264: theMaxHandlerThreads = Integer.parseInt(theProperties
265: .getProperty(aName + ".maxhandlerthreads"));
266: } catch (Exception e) {
267: Log
268: .getLog(theName)
269: .log(
270: "Couldn't parse '"
271: + aName
272: + ".maxhandlerthreads'. Using default value: "
273: + theMaxHandlerThreads);
274: }
275:
276: int theMinHandlerThreads = 1;
277:
278: try {
279: theMinHandlerThreads = Integer.parseInt(theProperties
280: .getProperty(aName + ".minhandlerthreads"));
281: } catch (Exception e) {
282: Log
283: .getLog(theName)
284: .log(
285: "Couldn't parse '"
286: + aName
287: + ".minhandlerthreads'. Using default value: "
288: + theMinHandlerThreads);
289: }
290:
291: int theSoTimeout = 30000;
292:
293: try {
294: theSoTimeout = Integer.parseInt(theProperties
295: .getProperty(aName + ".so_timeout"));
296: } catch (Exception e) {
297: Log.getLog(theName).log(
298: "Couldn't parse '" + aName
299: + ".so_timeout'. Using default value: "
300: + theSoTimeout);
301: }
302:
303: String theHandlerClassname = theProperties
304: .getProperty(aName + ".handlerclassname");
305:
306: if (theHandlerClassname == null) {
307: theHandlerClassname = "com.tagtraum.metaserver.Handler";
308:
309: Log
310: .getLog(theName)
311: .log(
312: "Couldn't parse '"
313: + aName
314: + ".handlerclassname'. Using default value: "
315: + theHandlerClassname);
316: }
317:
318: String theValidAddresses = theProperties.getProperty(aName
319: + ".validaddresses");
320: Vector theAddresses = parseAddresses(theValidAddresses);
321: String theServerClassname = theProperties.getProperty(aName
322: + ".classname");
323:
324: if (theServerClassname == null) {
325: theServerClassname = "com.tagtraum.metaserver.MetaServer";
326: }
327:
328: if (theService == null) {
329: theService = (MetaServer) Class.forName(
330: theServerClassname).newInstance();
331: }
332:
333: theService.setName(theName);
334: theService.setHandlerClassname(theHandlerClassname);
335: theService.setMaxHandlerThreads(theMaxHandlerThreads);
336: theService.setMinHandlerThreads(theMinHandlerThreads);
337: theService.setSoTimeout(theSoTimeout);
338: theService.setMajorVersion(theMajorVersion);
339: theService.setMinorVersion(theMinorVersion);
340:
341: for (int i = 0; i < theAddresses.size(); i++) {
342: theService.addValidAddress((InetAddress) theAddresses
343: .get(i));
344: }
345:
346: // add builders
347: Enumeration e = theProperties.propertyNames();
348:
349: while (e.hasMoreElements()) {
350: String key = (String) e.nextElement();
351:
352: if (key.startsWith(aName + ".service.")
353: && key.endsWith(".builderclass")
354: && key.indexOf('.') < key.lastIndexOf('.')) {
355: try {
356: I_Builder theBuilder = (I_Builder) Class
357: .forName(theProperties.getProperty(key))
358: .newInstance();
359: String theBuilderName = key.substring(aName
360: .length() + 1, key.lastIndexOf("."));
361:
362: theBuilder.setName(theName);
363:
364: String theURLString = theProperties
365: .getProperty(aName + ".service."
366: + theBuilderName + ".configdir");
367:
368: theBuilder.setURL(URLHelper.make(theURLString));
369: } catch (Throwable t) {
370: if (Log.isLog(theService.getName())) {
371: Log.getLog(theService.getName()).log(t);
372: }
373: }
374: }
375: }
376:
377: if (Log.isLog(theService.getName())) {
378: Log.getLog(theService.getName()).log(
379: "Built service '" + theName + "'.");
380: }
381:
382: return theService;
383: } catch (Throwable t) {
384: throw new BuildException(t);
385: }
386: }
387:
388: public void buildListeners(URL aURL, MetaServer aService) {
389: try {
390: Properties theProperties = loadProperties(aURL);
391: Enumeration e = theProperties.propertyNames();
392:
393: while (e.hasMoreElements()) {
394: String theKey = (String) e.nextElement();
395:
396: if (theKey.endsWith(".port")) {
397: int idx = theKey.indexOf('.');
398: String theName = theKey.substring(0, idx);
399: InetAddress theInetAddress = InetAddress
400: .getByName(theProperties
401: .getProperty(theName
402: + ".bindaddress"));
403: int thePort = 8080;
404:
405: try {
406: thePort = Integer.parseInt(theProperties
407: .getProperty(theName + ".port"));
408: } catch (Exception ex) {
409: Log
410: .getLog(aService.getName())
411: .log(
412: "Failed to parse '"
413: + theName
414: + ".port'. Using default value: "
415: + thePort);
416: }
417:
418: int theBacklog = 50;
419:
420: try {
421: theBacklog = Integer.parseInt(theProperties
422: .getProperty(theName + ".backlog"));
423: } catch (Exception ex) {
424: Log
425: .getLog(aService.getName())
426: .log(
427: "Failed to parse '"
428: + theName
429: + ".backlog'. Using default value: "
430: + theBacklog);
431: }
432:
433: I_TCPListener theListener = new TCPListener();
434:
435: theListener.setName(theName);
436: theListener.setPort(thePort);
437: theListener.setBacklog(theBacklog);
438: theListener.setBindAddress(theInetAddress);
439: theListener.setService(aService);
440: aService.addListener(theListener);
441:
442: if (Log.isLog(aService.getName())) {
443: Log.getLog(aService.getName()).log(
444: "Built listener '" + theName + "'.");
445: }
446: }
447: }
448: } catch (IOException ioe) {
449: Log.getLog(aService.getName()).log(ioe);
450: }
451: }
452:
453: public static final Properties loadProperties(URL aURL)
454: throws IOException {
455: Properties theProperties = new Properties();
456:
457: theProperties.load(aURL.openConnection().getInputStream());
458:
459: return theProperties;
460: }
461:
462: protected static final Vector parseAddresses(String anAdresses) {
463: if (anAdresses == null) {
464: return new Vector();
465: }
466:
467: Vector myValidAddresses = new Vector();
468: for (StringTokenizer st = new StringTokenizer(anAdresses, ", ;"); st
469: .hasMoreTokens();) {
470: try {
471: myValidAddresses.addElement(InetAddress.getByName(st
472: .nextToken()));
473: } catch (UnknownHostException uhe) {
474: uhe.printStackTrace();
475: }
476: }
477:
478: return myValidAddresses;
479: }
480:
481: }
|