1 /*
  2  * Copyright (c) 2002,2003 Peter Antman, Teknik i Media  <peter.antman@tim.se>
  3  *
  4  * This library is free software; you can redistribute it and/or
  5  * modify it under the terms of the GNU Lesser General Public
  6  * License as published by the Free Software Foundation; either
  7  * version 2 of the License, or (at your option) any later version
  8  *
  9  * This library is distributed in the hope that it will be useful,
 10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 12  * Lesser General Public License for more details.
 13  *
 14  * You should have received a copy of the GNU Lesser General Public
 15  * License along with this library; if not, write to the Free Software
 16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 17 */
 18 package org.xmlBlaster.j2ee.jmx;
 19 import java.util.Properties;
 20 
 21 import javax.naming.InitialContext;
 22 import javax.naming.Context;
 23 import javax.naming.Name;
 24 import javax.naming.NameNotFoundException;
 25 import javax.naming.NamingException;
 26 
 27 import org.xmlBlaster.util.Global;
 28 import org.xmlBlaster.util.EmbeddedXmlBlaster;
 29 import org.xmlBlaster.j2ee.util.JacorbUtil;
 30 import org.xmlBlaster.j2ee.util.GlobalUtil;
 31 import java.util.logging.Logger;
 32 /**
 33  * XmlBlaster for embedded use in a JMX server.
 34  *
 35  * <p>You may use this MBean to start one or more XmlBlaster instances in a
 36  JMX container. It has, however, only been tested with the JBoss 3.0 server. To start it in JBoss copy the xmlBlaster.sar archive into deploy. If you need to change the settings either edit the enbedded xmlBlaster.properties file or change the name of the property file in META-INF/jboss-service.xml and make it availabl1e in the XmlBlaster search path or embed it in the sar.</p>
 37 <p>To get better control ower the XmlBlaster setup process, the xmlBlaster.jar
 38 that's embedded in the sar has had its xmlBlaster.properties and xmlBlasterPlugins.xml files removed. It's recomended to do this also in any xmlBlaster.jar that is placed in the global classpath of JBoss, otherwise it might screw up client.</p>
 39 
 40 <h3>Requirements</h3>
 41 <p>.You need to copy the file concurrent.jar from xmlBlaster/lib to the system lib directory of JBoss, overwriting the older version distributed with JBoss.</p><p>When using the RMIDriver JBoss must be run with a security policy file specified, eg, sh run.sh -Djava.security.policy=../server/default/conf/server.policy.</p>
 42 <h3>Local clients</h3>
 43 <p>It is possible to use local client (in vm) clients by specifying a jndiName
 44 where a @link { org.xmlBlaster.j2ee.util.GlobalLookup} will be bound. If a client in the same VM looks this object upp through jndi, it will have access to the server engine.Global, and it is therefore possible to use the in vm client protocol.</p>
 45 
 46 
 47  *
 48  *
 49  * @author Peter Antman
 50  * @version $Revision: 1.9 $ $Date: 2006-03-07 19:26:23 +0000 (Tue, 07 Mar 2006) $
 51  */
 52 
 53 public class XmlBlasterService implements XmlBlasterServiceMBean {
 54    private static final String ME = "XmlBlasterService";
 55    private EmbeddedXmlBlaster blaster = null;
 56    private Global glob;
 57    private String propFile;
 58    private static Logger log = Logger.getLogger(XmlBlasterService.class.getName());
 59    private String jndiName;
 60    private Properties args = new Properties();
 61    private GlobalUtil globalUtil;
 62 
 63    public XmlBlasterService() {
 64    }
 65 
 66 
 67 
 68    /**
 69     * Set the name of a propertyfile to read settings from.
 70     *
 71     * <p>if this option is set, all properties specifyed in it will <i>overwrite</i> any properties sett on this MBean, since the file will be loaded last.</p>
 72     * <p>The context classloader will be searched first, then normal XmlBlaster search algoritm will be used.</p>
 73     */
 74    public void setPropertyFileName(String fileName) {
 75       propFile = fileName;
 76    }
 77 
 78    public String getPropertyFileName() {
 79       return propFile;
 80    }
 81 
 82    /**
 83     * Set the bootstrap port the instance should run at.
 84     * @param port Default bootstrapPort is 3412
 85     */
 86    public void setPort(String port) {
 87       args.setProperty("bootstrapPort", port);
 88    }
 89 
 90    public String getPort() {
 91       return args.getProperty("bootstrapPort");
 92    }
 93 
 94    /**
 95     * Set a JNDI name where a GlobalUtil will be bound.
 96     */
 97    public void setJNDIName(String jndiName) {
 98       this.jndiName = jndiName;
 99    }
100 
101    public String getJNDIName() {
102       return jndiName;
103    }
104    
105    /**
106     * Start the embedded XmlBlaster.
107     */
108    public void start() throws Exception {
109       globalUtil = new GlobalUtil();
110       glob = globalUtil.newGlobal(propFile,args );
111 
112       loadJacorbProperties();
113       globalUtil.setupSecurityManager(glob);
114 
115 
116       log.info("Starting XmlBlasterService");
117 
118       blaster = EmbeddedXmlBlaster.startXmlBlaster(glob);
119 
120       if ( jndiName != null) {
121          bind( blaster.getMain().getGlobal() );
122       } // end of if ()
123    }
124    
125    public void stop() throws Exception {
126       log.info("Stopping XmlBlaster service");
127       if (blaster != null ) {
128          EmbeddedXmlBlaster.stopXmlBlaster(blaster);
129       } // end of if ()
130       if ( jndiName != null) {
131          new InitialContext().unbind(jndiName);
132       } // end of if ()
133       
134    }
135 
136    public String dumpProperties() {
137       if ( glob == null) {
138          return "";
139       } // end of if ()
140       
141       return glob.getProperty().toXml();
142    }
143 
144    /**
145     * Bind a GlobalLookup into jndi.
146     */
147    private void bind(org.xmlBlaster.engine.ServerScope engineGlobal) throws Exception{
148       if ( jndiName == null) {
149          return;
150       } // end of if ()
151       
152       // Do we have JNDI at all?
153       Context ctx = null;
154       try {
155          ctx =  new InitialContext();
156       } catch (NamingException e) {
157          throw new IllegalStateException("No NamingContext available, trying to run with a jndiName in a server withouth jndi is not valid: "+e);
158       } // end of try-catch
159       
160       GlobalUtil gu = new GlobalUtil(engineGlobal);
161       bind(ctx,jndiName,gu);
162 
163    }
164 
165 
166    /**
167     * Jacorb is not capable of finding its jacorb.properties in the
168     * context classpath (actually it uses the system classloader.
169     * Remember that jacorb.properties is in xmlBlaster.jar.
170     */
171    private void loadJacorbProperties() throws Exception {
172       JacorbUtil.loadJacorbProperties("jacorb.properties",glob);
173    }
174 
175 
176    public void  bind(Context ctx, String name, Object val) throws NamingException
177    {
178       // Bind val to name in ctx, and make sure that all intermediate contexts exist
179       Name n = ctx.getNameParser("").parse(name);
180       while (n.size() > 1)
181       {
182          String ctxName = n.get(0);
183          try
184          {
185             ctx = (Context)ctx.lookup(ctxName);
186          } catch (NameNotFoundException e)
187          {
188             ctx = ctx.createSubcontext(ctxName);
189          }
190          n = n.getSuffix(1);
191       }
192 
193       ctx.bind(n.get(0), val);
194    }
195    
196 }


syntax highlighted by Code2HTML, v. 0.9.1