View Javadoc
1 /* 2 * Copyright (C) The PhoenixJMS Project. All rights reserved. 3 * 4 * This software is published under the terms of the Apache Software License 5 * version 1.1, a copy of which has been included with this distribution in 6 * the LICENSE file. 7 */ 8 package net.sf.phoenixjms.server.impl; 9 10 // apache 11 import org.apache.avalon.framework.context.Contextualizable; 12 import org.apache.avalon.framework.activity.Initializable; 13 import org.apache.avalon.framework.configuration.Configurable; 14 import org.apache.avalon.framework.configuration.Configuration; 15 import org.apache.avalon.framework.context.Context; 16 import org.apache.avalon.framework.context.ContextException; 17 import org.apache.avalon.framework.configuration.ConfigurationException; 18 import org.apache.avalon.framework.configuration.DefaultConfigurationSerializer; 19 import org.apache.avalon.framework.logger.Logger; 20 21 // IO 22 import java.io.OutputStream; 23 import java.io.File; 24 import java.io.FileOutputStream; 25 import java.io.ByteArrayOutputStream; 26 27 import org.exolab.jms.server.EmbeddedJmsServer; 28 import org.exolab.jms.server.ServerException; 29 30 import EDU.oswego.cs.dl.util.concurrent.Executor; 31 import EDU.oswego.cs.dl.util.concurrent.ThreadedExecutor; 32 33 import net.sf.phoenixjms.server.JMSServer; 34 import org.apache.avalon.framework.logger.AbstractLogEnabled; 35 36 /*** 37 * OpenJMS 0.7.5 wrapper for Avalon and Pico Containers. 38 * 39 * @author Alexis Agahi <alag@users.sourceforge.net> 40 * @author J Aaron Farr 41 * @version 0.9.0 42 * 43 * @avalon.component name="jms-server" version="1.0" lifestyle="singleton" 44 * @avalon.service type="net.sf.phoenixjms.server.JMSServer" version="1.0" 45 * 46 * @todo there isn't a simple shutdown hook for the openjms server 47 */ 48 49 public class OpenJMSServer 50 extends AbstractLogEnabled 51 implements JMSServer, Contextualizable, Configurable, Initializable 52 { 53 54 private String m_workingDir = System.getProperty("merlin.home",System.getProperty("user.dir")); 55 private EmbeddedJmsServer m_server; 56 private String m_config; 57 58 private boolean m_init = false; 59 60 public OpenJMSServer() 61 { 62 } 63 64 /*** 65 * constructor for PicoContainer. Automatically initializes the server. 66 * @todo really should try to make the logger a commons logger, not avalon logger 67 * @param config the openjms.xml configuration file location 68 * @param logger an Avalon Framework Logger 69 * @throws java.lang.Exception 70 */ 71 public OpenJMSServer(String config, Logger logger) throws Exception { 72 m_config = config; 73 enableLogging(logger); 74 initialize(); 75 } 76 77 /*** 78 * contextualizes the server using an Avalon context. 79 * Specifically looks for a home or working directory with the key 80 * "app.home" or "urn:avalon:home" 81 * @param context an Avalon Context 82 * @avalon.entry key="urn:avalon:home" type="java.io.File" 83 */ 84 public void contextualize(Context context) { 85 86 // we need to catch the exceptions, but none are really fatal 87 // would be nice if we could test if the context contains the 88 // key before we check though. 89 90 try { 91 File home = null; 92 93 try { 94 home = (File) context.get("app.home"); 95 } 96 catch (ContextException ex) { 97 if(getLogger().isWarnEnabled()) 98 getLogger().warn("'app.home' key not found in context, trying 'urn:avalon:home'"); 99 try { 100 home = (File) context.get("urn:avalon:home"); 101 } 102 catch (ContextException ex1) { 103 if(getLogger().isWarnEnabled()) 104 getLogger().warn("'urn:avalon:home' key not found in context, using default 'user.home'"); 105 } 106 } 107 if (home != null) { 108 m_workingDir = home.getAbsolutePath(); 109 } 110 if(getLogger().isDebugEnabled()) 111 getLogger().debug("Setting working directory to: "+m_workingDir); 112 } 113 catch (Exception ex) { 114 if(getLogger().isWarnEnabled()) 115 getLogger().warn("Error attempting to establish home directory. Using defaults.",ex); 116 } 117 } 118 119 /*** 120 * configuration method for Avalon containers. See examples for valid 121 * configuration schema. 122 * @param configuration the Avalon configuration 123 * @throws ConfigurationException 124 */ 125 public void configure( final Configuration configuration ) 126 throws ConfigurationException 127 { 128 129 Configuration base = configuration.getChild("base-dir",true); 130 m_workingDir = base.getValue(m_workingDir); 131 132 Configuration conf = configuration.getChild("openjms-config",true); 133 String confURL = conf.getValue(null); 134 135 // if we have an external configuration file, let's use it 136 if(confURL != null) 137 138 m_config = m_workingDir + confURL; 139 140 // if not, we'll extract the openjms configuration from the 141 // avalon configuration file 142 else{ 143 144 DefaultConfigurationSerializer confSerializer = new 145 DefaultConfigurationSerializer(); 146 147 String baseFolder = m_workingDir + File.separator; 148 String filename = baseFolder + "openjms.xml"; 149 150 try { 151 File file = new File(filename); 152 OutputStream outputStream = new FileOutputStream(file); 153 confSerializer.serialize(outputStream, configuration.getChild("openjms")); 154 outputStream.close(); 155 156 file = new File(baseFolder + "log4j.xml"); 157 ByteArrayOutputStream bos = new ByteArrayOutputStream(); 158 confSerializer.serialize(bos, 159 configuration.getChild("log4j:configuration")); 160 bos.close(); 161 String out = bos.toString(); 162 int index = out.indexOf("<log4j:configuration"); 163 out = out.substring(0, index) 164 + "<!DOCTYPE log4j:configuration SYSTEM \"log4j.dtd\">" 165 + out.substring(index); 166 outputStream = new FileOutputStream(file); 167 outputStream.write(out.getBytes()); 168 outputStream.close(); 169 170 } 171 catch (org.xml.sax.SAXException se) { 172 if(getLogger().isErrorEnabled()) 173 getLogger().error("XML error generating openjms configuation",se); 174 } 175 catch (java.io.FileNotFoundException fe) { 176 if(getLogger().isErrorEnabled()) 177 getLogger().error("File error generating openjms configuation",fe); 178 } 179 catch (java.io.IOException ie) { 180 if(getLogger().isErrorEnabled()) 181 getLogger().error("IO error generating openjms configuation",ie); 182 } 183 184 m_config = filename; 185 } 186 187 } 188 189 /*** 190 * starts the embedded JMS server. The PicoContainer constructor automatically 191 * calls this method. 192 * @throws java.lang.Exception 193 * @todo should we use some other Thread API? 194 */ 195 public void initialize() 196 throws Exception 197 { 198 if(!m_init){ 199 try { 200 m_server = new EmbeddedJmsServer(m_config); 201 Executor executor = new ThreadedExecutor(); 202 executor.execute(m_server); 203 m_init = true; 204 } 205 catch (ServerException ex) { 206 if(getLogger().isErrorEnabled()) 207 getLogger().error("Error starting embedded server",ex); 208 } 209 catch (InterruptedException ex) { 210 if(getLogger().isErrorEnabled()) 211 getLogger().error("Thread error in JMS Server",ex); 212 } 213 } 214 } 215 216 217 public String getProviderName(){ 218 return "OpenJMS 0.7.5"; 219 } 220 221 222 }

This page was automatically generated by Maven