/*
License $Id: JoHost.java,v 1.9 2003/09/13 04:59:57 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.jo;
import com.tagtraum.framework.http.StatusCodes;
import com.tagtraum.framework.log.C_Log;
import com.tagtraum.framework.log.Log;
import com.tagtraum.framework.server.ServerException;
import com.tagtraum.framework.util.UnSyncStringBuffer;
import com.tagtraum.jo.builder.I_JoHostBuilder;
import com.tagtraum.jo.event.ChildModificationEvent;
import com.tagtraum.jo.event.ChildModificationListener;
import com.tagtraum.perf.util.FastReadSyncMap;
import java.io.File;
import java.io.IOException;
import java.util.*;
/**
* Represents a virtual host. Hosts manage {@link I_JoServletContextPeer}s
* and some host specific data.
*
* @author <a href="mailto:hs@tagtraum.com">Hendrik Schreiber</a>
* @version 1.1beta1 $Id: JoHost.java,v 1.9 2003/09/13 04:59:57 hendriks73 Exp $
*/
public class JoHost implements C_Jo, I_JoHost {
/**
* Source-Version
*/
public static String vcid = "$Id: JoHost.java,v 1.9 2003/09/13 04:59:57 hendriks73 Exp $";
/**
* Prefix rules.
*/
private Map myPrefixMatchRules;
/**
* This Host's Peers.
*/
private Map myServletContextPeers;
/**
* The Service.
*/
private I_JoServletService myService;
/**
* Server-Info
*/
private String myServerInfo;
/**
* This host's name.
*/
private String myName;
/**
* Hostnames for this host.
*/
private String[] myHostnames;
/**
* Builder.
*/
private I_JoHostBuilder myBuilder;
/**
* Access log.
*/
private Log accessLog;
/**
* Log.
*/
private Log log;
/**
* Directories where the wars reside.
*/
private Set warDirs;
/**
* Listeners for modification.
*/
private Set childModificationListeners;
private static ResourceBundle localStrings = ResourceBundle.getBundle("com.tagtraum.jo.localStrings");
/**
* Constructor for this host.
*/
public JoHost() {
try {
myServletContextPeers = new FastReadSyncMap(new HashMap());
myPrefixMatchRules = new FastReadSyncMap(new HashMap());
}
catch (CloneNotSupportedException cnse) {
// should not be possible
cnse.printStackTrace();
Log.getLog().log(cnse, C_Log.ERROR);
// FIXME!!! Unspecific RuntimeException is a NONO! (rik)
throw new RuntimeException(cnse.toString());
}
childModificationListeners = new HashSet();
warDirs = new HashSet();
}
/**
* Initializes this host.
*/
public void init() throws ServerException {
myService = null;
myName = null;
myHostnames = null;
myServerInfo = null;
myServletContextPeers.clear();
myPrefixMatchRules.clear();
childModificationListeners.clear();
warDirs.clear();
}
/**
* Sets the Builder for this host.
*/
public void setBuilder(I_JoHostBuilder builder) {
myBuilder = builder;
}
/**
* Gets the Builder for this host.
*/
public I_JoHostBuilder getBuilder() {
return myBuilder;
}
/**
* Rebuilds the host or its components if necessary.
*/
public void rebuild() {
autoDeploy();
if (getBuilder().needsRebuild()) {
if (getLog().isLog(C_Log.MODULE)) {
getLog().log(localStrings.getString("rebuilding_host") + this, C_Log.MODULE);
}
try {
getBuilder().rebuild(this);
}
catch (Exception e) {
if (getLog().isLog(C_Log.ERROR)) {
getLog().log(localStrings.getString("host_rebuilt_failed") + this, C_Log.ERROR);
getLog().log(e, C_Log.ERROR);
}
}
}
// if the host does not need to be rebuilt, maybe some
// peers need rebuilding...
else {
Iterator it = myServletContextPeers.values().iterator();
while (it.hasNext()) {
I_JoServletContextPeer peer = (I_JoServletContextPeer)it.next();
if (peer.needsRebuild()) peer.rebuild();
}
}
}
public void autoDeploy() {
try {
getBuilder().buildWebApps(this);
}
catch (Exception e) {
if (getLog().isLog(C_Log.ERROR)) {
getLog().log(localStrings.getString("auto_deploy_error") + e, C_Log.ERROR);
getLog().log(e, C_Log.ERROR);
}
}
}
/**
* Deletes all set hostnames and sets them new.
*
* @param aHostnames list of hostnames
*/
public void setHostnames(String[] aHostnames) {
myHostnames = aHostnames;
}
/**
* Sets the service of this host.
*
* @param aService the service
*/
public void setService(I_JoServletService aService) {
myService = aService;
}
/**
* Returns the service of this host.
*
* @return the service.
*/
public I_JoServletService getService() {
return myService;
}
/**
* Returns the symbolic name of this host.
*
* @return this host's name
*/
public String getName() {
return myName;
}
/**
* Sets the the symbolic name of this host and its accesslog/eventlog.
*
* @param aName this host's name
* @see #setAccessLog
* @see #setLog
*/
public void setName(String aName) {
myName = aName;
setAccessLog(Log.getLog(myName + "_access"));
setLog(Log.getLog(myName));
}
/**
* Returns the names of this host
*
* @return names of this host
*/
public String[] getHostnames() {
return myHostnames;
}
/**
* Returns the name of this server. Format: <pre><name>/<majorversion.<minorversion></pre>
*
* @return server name
*/
public String getServerInfo() {
if (myServerInfo == null) {
// cache it!
myServerInfo = myService.getServerInfo();
}
return myServerInfo;
}
/**
* Returns an attribute of this service.
*
* @param aName key for an attribute.
* @return the value of this attribute or <code>null</code>.
*/
public Object getAttribute(String aName) {
return myService.getAttribute(aName);
}
/**
* Returns an iterator over all registered {@link I_JoServletContextPeer}s.
*
* @return Iterator
*/
public Iterator servletContextPeers() {
HashSet peers = new HashSet();
peers.addAll(myServletContextPeers.values());
return peers.iterator();
}
/**
* Returns the MIME type of the file or <code>null</code> if
* the type is not known.
*
* @param file Name of the file
*/
public final String getMimeType(String file) {
return myService.getMimeType(file);
}
/**
* Adds a {@link I_JoServletContextPeer} to this host.
*
* @param aPeer a Peer
*/
public void addServletContextPeer(I_JoServletContextPeer aPeer) {
myServletContextPeers.put(aPeer.getName(), aPeer);
myPrefixMatchRules.put(aPeer.getContextPath(), aPeer);
fireChildModificationEvent(new ChildModificationEvent(this, aPeer, true));
}
/**
* Removes a {@link I_JoServletContextPeer} from this host.
*
* @param aName Name of the peer
*/
public void removeServletContextPeer(String aName) {
I_JoServletContextPeer aPeer = (I_JoServletContextPeer)myServletContextPeers.get(aName);
if (aPeer != null) {
myPrefixMatchRules.remove(aPeer.getContextPath());
myServletContextPeers.remove(aName);
// is this the right place for this? (rik)
/*
try {
aPeer.getJspEngine().forceClean();
}
catch (IOException ioe) {
log.log("Force clean failed.", C_Log.ERROR);
log.log(ioe, C_Log.ERROR);
}
*/
aPeer.destroy();
fireChildModificationEvent(new ChildModificationEvent(this, aPeer, false));
}
}
/**
* Returns a {@link I_JoServletContextPeer} for a name.
*
* @param aKey key for a {@link I_JoServletContextPeer}
* @return I_JoServletContextPeer
*/
public I_JoServletContextPeer getNamedServletContextPeer(String aKey) {
return (I_JoServletContextPeer)myServletContextPeers.get(aKey);
}
/**
* Returns a {@link I_JoServletContextPeer} for a URI.
*
* @param aURI URI
* @return I_JoServletContextPeer
*/
public I_JoServletContextPeer getServletContextPeer(String aURI) {
if (aURI == null) return null;
I_JoServletContextPeer theMatchingPeer = (I_JoServletContextPeer)myPrefixMatchRules.get(aURI);
int idx;
while (theMatchingPeer == null && (idx = aURI.lastIndexOf('/')) != -1) {
aURI = aURI.substring(0, idx);
theMatchingPeer = (I_JoServletContextPeer)myPrefixMatchRules.get(aURI);
}
return theMatchingPeer;
}
/**
* Returns a {@link I_JoServletContextPeer} for a request.
*
* @param aRequest Request
* @return I_JoServletContextPeer
*/
public final I_JoServletContextPeer getServletContextPeer(I_JoServletRequest aRequest) {
return getServletContextPeer(aRequest.getRequestURI());
}
/**
* Returns an errorpage.
*
* @param aStatusCode Statuscode
* @param aMessage message
* @return an errorpage.
*/
public String getErrorPage(int aStatusCode, String aMessage) {
// here we could insert a JSP...?! (rik)
UnSyncStringBuffer sb = new UnSyncStringBuffer();
sb.append("<h2>");
sb.append(aStatusCode);
sb.append(' ');
sb.append(StatusCodes.get(aStatusCode));
sb.append("</h2>");
if (aMessage != null) {
sb.append("<pre>");
sb.append(aMessage);
sb.append("</pre>");
}
return sb.toString();
}
/**
* Returns an errorpage.
*
* @param aStatusCode Statuscode
* @param aThrowable Throwable
* @return an errorpage.
*/
public String getErrorPage(int aStatusCode, Throwable aThrowable) {
return getErrorPage(aStatusCode, Log.getStackTrace(aThrowable));
}
/**
* Returns the eventlog.
*
* @see #getAccessLog()
*/
public Log getLog() {
return log;
}
/**
* Sets the log.
*
* @see #setName(String)
*/
public void setLog(Log log) {
this.log = log;
}
/**
* Returns the accesslog.
*
* @see #getLog()
*/
public Log getAccessLog() {
return accessLog;
}
/**
* Sets the accesslog.
*
* @see #setName(String)
*/
public void setAccessLog(Log log) {
accessLog = log;
}
/**
* Returns the name of the directory where the wars are stored.
* JO_HOME/webapp/<hostname>/
*/
public File[] getWARDirs() {
return (File[])warDirs.toArray(new File[warDirs.size()]);
}
/**
* Adds the name of the directory where the wars are stored.
*/
public void addWARDir(File warDir) {
warDirs.add(warDir);
if (log.isLog(C_Log.MODULE)) {
log.log("Added WAR dir = " + warDir, C_Log.MODULE);
}
}
/**
* Removes the name of the directory where the wars are stored.
*/
public void removeWARDir(File warDir) {
warDirs.remove(warDir);
if (log.isLog(C_Log.MODULE)) {
log.log("Removed WAR dir = " + warDir, C_Log.MODULE);
}
}
/**
* Destroys all {@link I_JoServletContextPeer}s.
*/
public void destroy() {
Iterator i = myServletContextPeers.entrySet().iterator();
while (i.hasNext()) {
((I_JoServletContextPeer)((Map.Entry)i.next()).getValue()).destroy();
}
myServletContextPeers.clear();
try {
accessLog.flush();
log.flush();
}
catch (IOException ioe) {
Log.getLog().log(localStrings.getString("log_flush_failed") + ioe.toString());
}
}
protected synchronized void fireChildModificationEvent(ChildModificationEvent e) {
Iterator i = childModificationListeners.iterator();
while (i.hasNext()) {
((ChildModificationListener)i.next()).childModification(e);
}
}
public synchronized void addChildModificationListener(ChildModificationListener listener) {
childModificationListeners.add(listener);
}
public synchronized void removeChildModificationListener(ChildModificationListener listener) {
childModificationListeners.remove(listener);
}
}
|