/*
License $Id: Builder.java,v 1.7 2005/02/10 18:25:53 hendriks73 Exp $
Copyright (c) 2001-2005 tagtraum industries.
LGPL
====
jo! is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
jo! is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
For LGPL see <http://www.fsf.org/copyleft/lesser.txt>
Sun license
===========
This release contains software by Sun Microsystems. Therefore
the following conditions have to be met, too. They apply to the
files
- lib/mail.jar
- lib/activation.jar
- lib/jsse.jar
- lib/jcert.jar
- lib/jaxp.jar
- lib/crimson.jar
- lib/servlet.jar
- lib/jnet.jar
- lib/jaas.jar
- lib/jaasmod.jar
contained in this release.
a. Licensee may not modify the Java Platform
Interface (JPI, identified as classes contained within the javax
package or any subpackages of the javax package), by creating additional
classes within the JPI or otherwise causing the addition to or modification
of the classes in the JPI. In the event that Licensee creates any
Java-related API and distribute such API to others for applet or
application development, you must promptly publish broadly, an accurate
specification for such API for free use by all developers of Java-based
software.
b. Software is confidential copyrighted information of Sun and
title to all copies is retained by Sun and/or its licensors. Licensee
shall not modify, decompile, disassemble, decrypt, extract, or otherwise
reverse engineer Software. Software may not be leased, assigned, or
sublicensed, in whole or in part. Software is not designed or intended
for use in on-line control of aircraft, air traffic, aircraft navigation
or aircraft communications; or in the design, construction, operation or
maintenance of any nuclear facility. Licensee warrants that it will not
use or redistribute the Software for such purposes.
c. Software is provided "AS IS," without a warranty
of any kind. ALL EXPRESS OR IMPLIED REPRESENTATIONS AND WARRANTIES,
INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED.
d. This License is effective until terminated. Licensee may
terminate this License at any time by destroying all copies of Software.
This License will terminate immediately without notice from Sun if Licensee
fails to comply with any provision of this License. Upon such termination,
Licensee must destroy all copies of Software.
e. Software, including technical data, is subject to U.S.
export control laws, including the U.S. Export Administration Act and its
associated regulations, and may be subject to export or import regulations
in other countries. Licensee agrees to comply strictly with all such
regulations and acknowledges that it has the responsibility to obtain
licenses to export, re-export, or import Software. Software may not be
downloaded, or otherwise exported or re-exported (i) into, or to a national
or resident of, Cuba, Iraq, Iran, North Korea, Libya, Sudan, Syria or any
country to which the U.S. has embargoed goods; or (ii) to anyone on the
U.S. Treasury Department's list of Specially Designated Nations or the U.S.
Commerce Department's Table of Denial Orders.
Feedback
========
We encourage your feedback and suggestions and want to use your feedback to
improve the Software. Send all such feedback to:
<feedback@tagtraum.com>
For more information on tagtraum industries and jo!
please see <http://www.tagtraum.com/>.
*/
package com.tagtraum.metaserver;
import com.tagtraum.framework.log.C_Log;
import com.tagtraum.framework.log.Log;
import com.tagtraum.framework.log.LogWriter;
import com.tagtraum.framework.server.I_TCPListener;
import com.tagtraum.framework.server.TCPListener;
import com.tagtraum.framework.util.*;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.net.InetAddress;
import java.net.URL;
import java.net.UnknownHostException;
import java.util.Enumeration;
import java.util.Properties;
import java.util.StringTokenizer;
import java.util.Vector;
/**
*
* @author Hendrik Schreiber
* @version 1.1beta1 $Id: Builder.java,v 1.7 2005/02/10 18:25:53 hendriks73 Exp $
*/
public class Builder extends AbstractBuilder {
/**
* Source-Version
*/
public static String vcid = "$Id: Builder.java,v 1.7 2005/02/10 18:25:53 hendriks73 Exp $";
/**
* Indicates whether the built object needs to be rebuilt.
*
* @return true or false
*/
public boolean needsRebuild() {
return false;
}
/**
* Rebuilds the object.
*
* @param obj object to rebuild
*/
public void rebuild(Object obj) throws BuildException {
MetaServer theService = (MetaServer)obj;
try {
Properties theProperties = loadProperties(new URL(getURL(), "metaserver.properties"));
buildService(theService, getName(), theProperties, getURL());
buildListeners(new URL(getURL(), "metalistener.properties"), theService);
}
catch (Throwable t) {
throw new BuildException(t);
}
}
/**
* Builds an object with a specified name.
*
* @return the object
*/
public Object build() throws BuildException {
MetaServer theService = null;
try {
Properties theProperties = loadProperties(new URL(getURL(), "metaserver.properties"));
theService = buildService(null, getName(), theProperties, getURL());
buildListeners(new URL(getURL(), "metalistener.properties"), theService);
}
catch (Throwable t) {
throw new BuildException(t);
}
return theService;
}
public MetaServer buildService(MetaServer theService, String aName, Properties theProperties, URL aURL) throws IOException, BuildException {
try {
String theName = theProperties.getProperty(aName + ".name");
if (theName == null) {
theName = "MetaServer";
Log.getLog(theName).log("Couldn't parse '" + aName + ".name'. Using default value: " + theName);
}
String theEventLogName = theProperties.getProperty(aName + ".log");
URL theLogURL = null;
if (theEventLogName == null) {
theLogURL = new URL(aURL, "metaserver.log");
} else if (!theEventLogName.equalsIgnoreCase("NONE")) {
theLogURL = URLHelper.make(aURL, StringHelper.replaceWithSystemProperty(theEventLogName));
}
if (theLogURL != null) {
try {
final File logFile = new File(PlatformHelper.getOSSpecificPath(theLogURL.getFile()));
logFile.getParentFile().mkdirs();
LogWriter theLogWriter = new LogWriter(new FileWriter(logFile, true), 1);
Log.getLog(theName).addI_LogEventListener(theLogWriter);
} catch (IOException ioe) {
Log.getLog(getName()).log("Failed to create metaserver log: " + ioe, C_Log.ERROR);
}
}
int theMajorVersion = 0;
try {
theMajorVersion = Integer.parseInt(theProperties.getProperty(aName + ".majorversion"));
}
catch (Exception e) {
Log.getLog(theName).log("Failed to parse '" + aName + ".majorversion'. Using default value: " + theMajorVersion);
}
int theMinorVersion = 8;
try {
theMinorVersion = Integer.parseInt(theProperties.getProperty(aName + ".minorversion"));
}
catch (Exception e) {
Log.getLog(theName).log("Couldn't parse '" + aName + ".minorversion'. Using default value: " + theMinorVersion);
}
int theMaxHandlerThreads = 1;
try {
theMaxHandlerThreads = Integer.parseInt(theProperties.getProperty(aName + ".maxhandlerthreads"));
}
catch (Exception e) {
Log.getLog(theName).log("Couldn't parse '" + aName + ".maxhandlerthreads'. Using default value: " + theMaxHandlerThreads);
}
int theMinHandlerThreads = 1;
try {
theMinHandlerThreads = Integer.parseInt(theProperties.getProperty(aName + ".minhandlerthreads"));
}
catch (Exception e) {
Log.getLog(theName).log("Couldn't parse '" + aName + ".minhandlerthreads'. Using default value: " + theMinHandlerThreads);
}
int theSoTimeout = 30000;
try {
theSoTimeout = Integer.parseInt(theProperties.getProperty(aName + ".so_timeout"));
}
catch (Exception e) {
Log.getLog(theName).log("Couldn't parse '" + aName + ".so_timeout'. Using default value: " + theSoTimeout);
}
String theHandlerClassname = theProperties.getProperty(aName + ".handlerclassname");
if (theHandlerClassname == null) {
theHandlerClassname = "com.tagtraum.metaserver.Handler";
Log.getLog(theName).log("Couldn't parse '" + aName + ".handlerclassname'. Using default value: " + theHandlerClassname);
}
String theValidAddresses = theProperties.getProperty(aName + ".validaddresses");
Vector theAddresses = parseAddresses(theValidAddresses);
String theServerClassname = theProperties.getProperty(aName + ".classname");
if (theServerClassname == null) {
theServerClassname = "com.tagtraum.metaserver.MetaServer";
}
if (theService == null) {
theService = (MetaServer)Class.forName(theServerClassname).newInstance();
}
theService.setName(theName);
theService.setHandlerClassname(theHandlerClassname);
theService.setMaxHandlerThreads(theMaxHandlerThreads);
theService.setMinHandlerThreads(theMinHandlerThreads);
theService.setSoTimeout(theSoTimeout);
theService.setMajorVersion(theMajorVersion);
theService.setMinorVersion(theMinorVersion);
for (int i = 0; i < theAddresses.size(); i++) {
theService.addValidAddress((InetAddress)theAddresses.get(i));
}
// add builders
Enumeration e = theProperties.propertyNames();
while (e.hasMoreElements()) {
String key = (String)e.nextElement();
if (key.startsWith(aName + ".service.") && key.endsWith(".builderclass") && key.indexOf('.') < key.lastIndexOf('.')) {
try {
I_Builder theBuilder = (I_Builder)Class.forName(theProperties.getProperty(key)).newInstance();
String theBuilderName = key.substring(aName.length() + 1, key.lastIndexOf("."));
theBuilder.setName(theName);
String theURLString = theProperties.getProperty(aName + ".service." + theBuilderName + ".configdir");
theBuilder.setURL(URLHelper.make(theURLString));
}
catch (Throwable t) {
if (Log.isLog(theService.getName())) {
Log.getLog(theService.getName()).log(t);
}
}
}
}
if (Log.isLog(theService.getName())) {
Log.getLog(theService.getName()).log("Built service '" + theName + "'.");
}
return theService;
}
catch (Throwable t) {
throw new BuildException(t);
}
}
public void buildListeners(URL aURL, MetaServer aService) {
try {
Properties theProperties = loadProperties(aURL);
Enumeration e = theProperties.propertyNames();
while (e.hasMoreElements()) {
String theKey = (String)e.nextElement();
if (theKey.endsWith(".port")) {
int idx = theKey.indexOf('.');
String theName = theKey.substring(0, idx);
InetAddress theInetAddress = InetAddress.getByName(theProperties.getProperty(theName + ".bindaddress"));
int thePort = 8080;
try {
thePort = Integer.parseInt(theProperties.getProperty(theName + ".port"));
}
catch (Exception ex) {
Log.getLog(aService.getName()).log("Failed to parse '" + theName + ".port'. Using default value: " + thePort);
}
int theBacklog = 50;
try {
theBacklog = Integer.parseInt(theProperties.getProperty(theName + ".backlog"));
}
catch (Exception ex) {
Log.getLog(aService.getName()).log("Failed to parse '" + theName + ".backlog'. Using default value: " + theBacklog);
}
I_TCPListener theListener = new TCPListener();
theListener.setName(theName);
theListener.setPort(thePort);
theListener.setBacklog(theBacklog);
theListener.setBindAddress(theInetAddress);
theListener.setService(aService);
aService.addListener(theListener);
if (Log.isLog(aService.getName())) {
Log.getLog(aService.getName()).log("Built listener '" + theName + "'.");
}
}
}
}
catch (IOException ioe) {
Log.getLog(aService.getName()).log(ioe);
}
}
public static final Properties loadProperties(URL aURL) throws IOException {
Properties theProperties = new Properties();
theProperties.load(aURL.openConnection().getInputStream());
return theProperties;
}
protected static final Vector parseAddresses(String anAdresses) {
if (anAdresses == null) {
return new Vector();
}
Vector myValidAddresses = new Vector();
for (StringTokenizer st = new StringTokenizer(anAdresses, ", ;"); st.hasMoreTokens();) {
try {
myValidAddresses.addElement(InetAddress.getByName(st.nextToken()));
}
catch (UnknownHostException uhe) {
uhe.printStackTrace();
}
}
return myValidAddresses;
}
}
|