View Javadoc

1   /*
2    * Copyright 2004-2005 Alvaro Gonzalez de Paz (kortsoft)
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License")
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package kortsoft.kmx.deployer;
17  
18  import java.io.File;
19  import java.net.MalformedURLException;
20  import java.net.URL;
21  import java.net.URLClassLoader;
22  import java.util.ArrayList;
23  import java.util.Hashtable;
24  
25  import javax.management.InstanceAlreadyExistsException;
26  import javax.management.MBeanRegistrationException;
27  import javax.management.MBeanServer;
28  import javax.management.MBeanServerFactory;
29  import javax.management.MalformedObjectNameException;
30  import javax.management.NotCompliantMBeanException;
31  import javax.management.ObjectName;
32  
33  import kortsoft.kmx.jmx.JmxConfig;
34  import kortsoft.kmx.jmx.ManagedClassLoader;
35  
36  /***
37   * Deploys a jar File in the server, adding a new Class Loader with the URL
38   * of the jar file.
39   * <a href="JarDeployer.java.html"><b><i>View Source</i></b></a>
40   *
41   * @author  Alvaro
42   * @version $Revision: 1.1 $
43   *
44   */
45  public class JarDeployer extends BaseDeployer implements Deployer {
46      static final String FILENAME_JMX_PROPERTY = "fileNane"; 
47      static final String TYPE_JMX_PROPERTY = "type";
48      private ClassLoader parent;
49      private MBeanServer server;
50  
51      /***
52       * Deploys the Jar File.
53       * @see kortsoft.kmx.deployer.Deployer#deployElement(java.io.File, java.io.File)
54       */
55      public void deployElement(File file){
56          URLClassLoader ucl;
57          try {
58              ucl=new URLClassLoader(new URL[]{file.toURL()},getParent());
59          } catch (MalformedURLException e) {
60              throw new FileNotDeployableException("Malformed URL file :"+file.getName());
61          }
62          ManagedClassLoader mcl=new ManagedClassLoader(ucl);
63          try {
64              getMBeanServer().registerMBean(mcl,newCLName(file.getName()));
65          } catch (InstanceAlreadyExistsException e1) {
66              instanceAlreadyExists(file.getName());
67          } catch (MBeanRegistrationException e1) {
68              notifyRegistrationProblem(this.getClass(),file,e1);
69          } catch (NotCompliantMBeanException e1) {
70              throw new RuntimeException("Unuexpected problem registering deployment: "+e1);
71          }
72      }
73      /***
74       * @return Returns the parent.
75       */
76      public ClassLoader getParent() {
77          return parent;
78      }
79      /***
80       * @param parent The parent to set.
81       */
82      public void setParent(ClassLoader parent) {
83          this.parent = parent;
84      }
85      /***
86       * Sets the mbean Server for registering deploys.
87       * If server is null or not setted, the first MBeanServer
88       * is taken.
89       * @see kortsoft.kmx.deployer.Deployer#setMBeanServer(javax.management.MBeanServer)
90       */
91      public void setMBeanServer(MBeanServer server) {
92          this.server=server;
93      }
94      protected MBeanServer getMBeanServer(){
95          if (server==null)
96              server=findDefaulMBeanServer();
97          return server;
98      }
99      /***
100      * @param name
101      * @return
102      */
103     private ObjectName newCLName(String name) {        
104         Hashtable<String,String> table=new Hashtable<String,String>(2);
105         table.put(FILENAME_JMX_PROPERTY,name);
106         table.put(TYPE_JMX_PROPERTY,"jar");
107         String domain=JmxConfig.getClassLoaderDomainName();
108         ObjectName oName;
109         try {
110             oName = new ObjectName(domain,table);
111         } catch (MalformedObjectNameException e) {
112            throw new RuntimeException("Malformed name for classloader. Domain: "
113                    +domain+" FileName:"+name,e);
114         } 
115         return oName;
116     }    
117     /***
118      * @return
119      */
120     private MBeanServer findDefaulMBeanServer() {
121         ArrayList<MBeanServer> list = 
122             (ArrayList<MBeanServer>)MBeanServerFactory.findMBeanServer(null);
123         return list.get(0);
124     }
125 
126 }