Home » geronimo-eclipse-plugin-2.2-source-release » org.apache.geronimo.st.v11.core » [javadoc | source]

    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    *
    9    *    http://www.apache.org/licenses/LICENSE-2.0
   10    *
   11    * Unless required by applicable law or agreed to in writing, software
   12    * distributed under the License is distributed on an "AS IS" BASIS,
   13    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   14    * See the License for the specific language governing permissions and
   15    * limitations under the License.
   16    */
   17   package org.apache.geronimo.st.v11.core;
   18   
   19   import java.util.ArrayList;
   20   import java.util.Arrays;
   21   import java.util.Collections;
   22   import java.util.Iterator;
   23   import java.util.List;
   24   import java.util.Map;
   25   import java.util.Set;
   26   import java.util.TreeSet;
   27   
   28   import javax.management.MBeanServerConnection;
   29   
   30   import org.apache.geronimo.gbean.AbstractName;
   31   import org.apache.geronimo.gbean.AbstractNameQuery;
   32   import org.apache.geronimo.gbean.GAttributeInfo;
   33   import org.apache.geronimo.gbean.GBeanInfo;
   34   import org.apache.geronimo.kernel.GBeanNotFoundException;
   35   import org.apache.geronimo.kernel.InternalKernelException;
   36   import org.apache.geronimo.kernel.Kernel;
   37   import org.apache.geronimo.kernel.NoSuchAttributeException;
   38   import org.apache.geronimo.kernel.NoSuchOperationException;
   39   import org.apache.geronimo.kernel.repository.Artifact;
   40   import org.apache.geronimo.st.core.GeronimoServerBehaviourDelegate;
   41   import org.apache.geronimo.st.v11.core.internal.Trace;
   42   import org.apache.geronimo.system.jmx.KernelDelegate;
   43   import org.apache.geronimo.xml.ns.deployment_1.DependencyType;
   44   import org.apache.geronimo.xml.ns.naming_1.PatternType;
   45   import org.eclipse.core.runtime.IProgressMonitor;
   46   import org.eclipse.core.runtime.IStatus;
   47   import org.eclipse.core.runtime.Status;
   48   import org.eclipse.core.runtime.jobs.Job;
   49   import org.eclipse.wst.server.core.IServer;
   50   import org.eclipse.wst.server.core.ServerCore;
   51   
   52   /**
   53    * @version $Rev: 688452 $ $Date: 2008-08-24 13:56:20 +0800 (Sun, 24 Aug 2008) $
   54    */
   55   public class GeronimoServerInfo {
   56       static final long serialVersionUID = 1L;
   57   
   58       private ArrayList<Kernel> kernels;
   59   
   60       private ArrayList<PatternType> deployedEJBs;
   61       private ArrayList<String> securityRealms;
   62       private ArrayList<PatternType> jmsConnectionFactories;
   63       private ArrayList<PatternType> jmsDestinations;
   64       private ArrayList<PatternType> jdbcConnectionPools;
   65       private ArrayList<PatternType> javaMailResources;
   66       private ArrayList<org.apache.geronimo.xml.ns.deployment_1.PatternType> credentialStores;
   67       private ArrayList<DependencyType> commonLibs;
   68   
   69       // singleton class
   70       private GeronimoServerInfo() {
   71       }
   72   
   73       private static GeronimoServerInfo instance = new GeronimoServerInfo();
   74   
   75       public static GeronimoServerInfo getInstance() {
   76           return instance;
   77       }
   78   
   79       public ArrayList<PatternType> getDeployedEjbs() {
   80           return deployedEJBs;
   81       }
   82   
   83       public ArrayList<String> getSecurityRealms() {
   84           return securityRealms;
   85       }
   86   
   87       public ArrayList<PatternType> getJmsConnectionFactories() {
   88           return jmsConnectionFactories;
   89       }
   90   
   91       public ArrayList<PatternType> getJmsDestinations() {
   92           return jmsDestinations;
   93       }
   94   
   95       public ArrayList<PatternType> getJdbcConnectionPools() {
   96           return jdbcConnectionPools;
   97       }
   98   
   99       public ArrayList<PatternType> getJavaMailSessions() {
  100           return javaMailResources;
  101       }
  102   
  103       public ArrayList<org.apache.geronimo.xml.ns.deployment_1.PatternType> getDeployedCredentialStores() {
  104           return credentialStores;
  105       }
  106   
  107       public ArrayList<DependencyType> getCommonLibs() {
  108           return commonLibs;
  109       }
  110   
  111       public void updateInfo() {
  112           Job job = new Job("Getting Dynamic Information from Server"){
  113               protected IStatus run(IProgressMonitor arg0) {
  114                   updateKernels();
  115                   updateDeployedEJBs();
  116                   updateSecurityRealms();
  117                   updateJmsConnectionFactories();
  118                   updateJmsDestinations();
  119                   updateJdbcConnectionPools();
  120                   updateJavaMailSessions();
  121                   updateDeployedCredentialStores();
  122                   updateCommonLibs();
  123                   return Status.OK_STATUS;
  124               }
  125           };
  126           job.setPriority(Job.SHORT);
  127           job.schedule();
  128       }
  129   
  130       private void updateKernels() {
  131           kernels = new ArrayList<Kernel>();
  132           IServer[] servers = ServerCore.getServers();
  133           for (int i = 0; i < servers.length; i++) {
  134               if (servers[i].getServerState() == IServer.STATE_STARTED) {
  135                   try {
  136                       GeronimoServerBehaviourDelegate delegate = (GeronimoServerBehaviourDelegate) servers[i]
  137                               .getAdapter(GeronimoServerBehaviourDelegate.class);
  138                       if (delegate != null) {
  139                           MBeanServerConnection connection = delegate
  140                                   .getServerConnection();
  141                           if (connection != null) {
  142                               kernels.add(new KernelDelegate(connection));
  143                           }
  144                       }
  145                   } catch (SecurityException e) {
  146                   } catch (Exception e) {
  147                       Trace.trace(Trace.WARNING, "Kernel connection failed. "
  148                               + e.getMessage());
  149                   }
  150               }
  151           }
  152       }
  153   
  154       private void updateDeployedEJBs() {
  155           deployedEJBs = new ArrayList<PatternType>();
  156           ArrayList<PatternType> statelessSessionBeans = getByType("StatelessSessionBean");
  157           ArrayList<PatternType> statefulSessionBeans = getByType("StatefulSessionBean");
  158           ArrayList<PatternType> messageDrivenBeans = getByType("MessageDrivenBean");
  159           ArrayList<PatternType> entityBeans = getByType("EntityBean");
  160           deployedEJBs.addAll(statelessSessionBeans);
  161           deployedEJBs.addAll(statefulSessionBeans);
  162           deployedEJBs.addAll(messageDrivenBeans);
  163           deployedEJBs.addAll(entityBeans);
  164       }
  165   
  166       private void updateSecurityRealms() {
  167           securityRealms = new ArrayList<String>();
  168           Map map = Collections.singletonMap("j2eeType", "SecurityRealm");
  169           AbstractNameQuery query = new AbstractNameQuery(null, map,
  170                   Collections.EMPTY_SET);
  171           for (int i = 0; i < kernels.size(); i++) {
  172               Set beans = kernels.get(i).listGBeans(query);
  173               for (Iterator it = beans.iterator(); it.hasNext();) {
  174                   AbstractName abstractName = (AbstractName) it.next();
  175                   String name = (String) abstractName.getName().get("name");
  176                   if (!securityRealms.contains(name)) {
  177                       securityRealms.add(name);
  178                   }
  179               }
  180           }
  181       }
  182   
  183       private void updateJmsConnectionFactories() {
  184           String[] requiredInterfaces = new String[] {
  185                   "javax.jms.ConnectionFactory",
  186                   "javax.jms.QueueConnectionFactory",
  187                   "javax.jms.TopicConnectionFactory", };
  188           String attribute = "implementedInterfaces";
  189           jmsConnectionFactories = getByTypeAttributeValues(
  190                   "JCAManagedConnectionFactory", attribute, requiredInterfaces);
  191       }
  192   
  193       private void updateJmsDestinations() {
  194           String[] requiredInterfaces = new String[] { "javax.jms.Queue",
  195                   "javax.jms.Topic" };
  196           String attribute = "adminObjectInterface";
  197           jmsDestinations = getByTypeAttributeValues("JCAAdminObject", attribute,
  198                   requiredInterfaces);
  199       }
  200   
  201       private void updateJdbcConnectionPools() {
  202           String[] acceptedValues = new String[] { "javax.sql.DataSource" };
  203           String attribute = "connectionFactoryInterface";
  204           jdbcConnectionPools = getByTypeAttributeValues(
  205                   "JCAManagedConnectionFactory", attribute, acceptedValues);
  206   
  207       }
  208   
  209       private void updateJavaMailSessions() {
  210           javaMailResources = getByType("JavaMailResource");
  211       }
  212   
  213       private void updateDeployedCredentialStores() {
  214           credentialStores = new ArrayList<org.apache.geronimo.xml.ns.deployment_1.PatternType>();
  215           Map map = Collections.singletonMap("j2eeType", "GBean");
  216           AbstractNameQuery query = new AbstractNameQuery(null, map,
  217                   Collections.EMPTY_SET);
  218           for (int i = 0; i < kernels.size(); i++) {
  219               Set beans = kernels.get(i).listGBeans(query);
  220               for (Iterator it = beans.iterator(); it.hasNext();) {
  221                   AbstractName abstractName = (AbstractName) it.next();
  222                   try {
  223                       GBeanInfo info = kernels.get(i).getGBeanInfo(abstractName);
  224                       GAttributeInfo attribInfo = info
  225                               .getAttribute("credentialStore");
  226                       if (attribInfo != null) {
  227                           Artifact artifact = abstractName.getArtifact();
  228                           Object name = abstractName.getName().get("name");
  229                           org.apache.geronimo.xml.ns.deployment_1.PatternType pattern = new org.apache.geronimo.xml.ns.deployment_1.PatternType();
  230                           pattern.setArtifactId(artifact.getArtifactId());
  231                           pattern.setGroupId(artifact.getGroupId());
  232                           pattern.setType(artifact.getType());
  233                           pattern.setVersion(artifact.getVersion().toString());
  234                           pattern.setCustomFoo((String) name);
  235                           if (!credentialStores.contains(pattern)) {
  236                               credentialStores.add(pattern);
  237                           }
  238                       }
  239                   } catch (GBeanNotFoundException e) {
  240                   } catch (Exception e) {
  241                       e.printStackTrace();
  242                   }
  243               }
  244           }
  245       }
  246   
  247       private void updateCommonLibs() {
  248           List<Object> artifacts = null;
  249           commonLibs = new ArrayList<DependencyType>();
  250           Map map = Collections.singletonMap("j2eeType", "Repository");
  251           AbstractNameQuery query = new AbstractNameQuery(null, map,
  252                   Collections.EMPTY_SET);
  253           for (int i = 0; i < kernels.size(); i++) {
  254               Set beans = kernels.get(i).listGBeans(query);
  255               for (Iterator it = beans.iterator(); it.hasNext();) {
  256                   AbstractName abstractName = (AbstractName) it.next();
  257                   try {
  258                       GBeanInfo info = kernels.get(i).getGBeanInfo(abstractName);
  259                       Object value = kernels.get(i).invoke(abstractName, "list");
  260                       if (value instanceof TreeSet) {
  261                           artifacts = Arrays.asList(((TreeSet) value).toArray());
  262                       }
  263                   } catch (GBeanNotFoundException e) {
  264                       Trace.trace(Trace.WARNING, "GBean Not Found. "
  265                               + e.getMessage());
  266                   } catch (NoSuchOperationException e) {
  267                       Trace.trace(Trace.WARNING, "The operation cant invoked. "
  268                               + e.getMessage());
  269                   } catch (InternalKernelException e) {
  270                       throw e;
  271                   } catch (Exception e) {
  272                       Trace.trace(Trace.WARNING, "Kernel connection failed.  "
  273                               + e.getMessage());
  274                   }
  275   
  276               }
  277           }
  278           if (artifacts != null) {
  279               for (int i = 0; i < artifacts.size(); i++) {
  280                   DependencyType dependency = new DependencyType();
  281                   dependency.setArtifactId(((Artifact) artifacts.get(i))
  282                           .getArtifactId());
  283                   dependency.setGroupId(((Artifact) artifacts.get(i))
  284                           .getGroupId());
  285                   dependency.setVersion(((Artifact) artifacts.get(i))
  286                           .getVersion().toString());
  287                   dependency.setType(((Artifact) artifacts.get(i)).getType());
  288                   if (!commonLibs.contains(dependency)) {
  289                       commonLibs.add(dependency);
  290                   }
  291               }
  292           }
  293       }
  294   
  295       private ArrayList<PatternType> getByTypeAttributeValues(String type,
  296               String attribute, String[] acceptedValues) {
  297           ArrayList<PatternType> result = new ArrayList<PatternType>();
  298           Map map = Collections.singletonMap("j2eeType", type);
  299           AbstractNameQuery query = new AbstractNameQuery(null, map,
  300                   Collections.EMPTY_SET);
  301           for (int i = 0; i < kernels.size(); i++) {
  302               Set beans = kernels.get(i).listGBeans(query);
  303               for (Iterator it = beans.iterator(); it.hasNext();) {
  304                   AbstractName abstractName = (AbstractName) it.next();
  305                   try {
  306                       Object value = kernels.get(i).getAttribute(abstractName,
  307                               attribute);
  308                       if (value != null) {
  309                           if (value instanceof String[]) {
  310                               List<String> interfaces = Arrays
  311                                       .asList((String[]) value);
  312                               for (int j = 0; j < acceptedValues.length; j++) {
  313                                   if (interfaces.contains(acceptedValues[j])) {
  314                                   	PatternType pattern = new PatternType();
  315                                       Artifact artifact = abstractName
  316                                               .getArtifact();
  317                                       pattern.setArtifactId(artifact
  318                                               .getArtifactId());
  319                                       pattern.setGroupId(artifact.getGroupId());
  320                                       pattern.setVersion(artifact.getVersion()
  321                                               .toString());
  322                                       pattern.setName((String) abstractName
  323                                               .getName().get("name"));
  324                                       if (!result.contains(pattern)) {
  325                                           result.add(pattern);
  326                                       }
  327                                       break;
  328                                   }
  329                               }
  330                           }
  331                           if (value instanceof String) {
  332                               String interfaces = (String) value;
  333                               for (int j = 0; j < acceptedValues.length; j++) {
  334                                   if (interfaces.contains(acceptedValues[j])) {
  335                                   	PatternType pattern = new PatternType();
  336                                       Artifact artifact = abstractName
  337                                               .getArtifact();
  338                                       pattern.setArtifactId(artifact
  339                                               .getArtifactId());
  340                                       pattern.setGroupId(artifact.getGroupId());
  341                                       pattern.setVersion(artifact.getVersion()
  342                                               .toString());
  343                                       pattern.setName((String) abstractName
  344                                               .getName().get("name"));
  345                                       if (!result.contains(pattern)) {
  346                                           result.add(pattern);
  347                                       }
  348                                       break;
  349                                   }
  350                               }
  351                           }
  352                       }
  353                   } catch (GBeanNotFoundException e) {
  354                   } catch (NoSuchAttributeException e) {
  355                   } catch (Exception e) {
  356                       Trace.trace(Trace.WARNING, "Kernel connection failed. "
  357                               + e.getMessage());
  358                   }
  359               }
  360           }
  361           return result;
  362       }
  363   
  364       private ArrayList<PatternType> getByType(String type) {
  365           ArrayList<PatternType> result = new ArrayList<PatternType>();
  366           Map map = Collections.singletonMap("j2eeType", type);
  367           AbstractNameQuery query = new AbstractNameQuery(null, map,
  368                   Collections.EMPTY_SET);
  369           for (int i = 0; i < kernels.size(); i++) {
  370               Set beans = kernels.get(i).listGBeans(query);
  371               for (Iterator it = beans.iterator(); it.hasNext();) {
  372                   AbstractName abstractName = (AbstractName) it.next();
  373                   PatternType pattern = new PatternType();
  374                   Artifact artifact = abstractName.getArtifact();
  375                   pattern.setArtifactId(artifact.getArtifactId());
  376                   pattern.setGroupId(artifact.getGroupId());
  377                   pattern.setVersion(artifact.getVersion().toString());
  378                   pattern.setName((String) abstractName.getName().get("name"));
  379                   if (!result.contains(pattern)) {
  380                       result.add(pattern);
  381                   }
  382               }
  383           }
  384           return result;
  385       }
  386   
  387       protected void printNamingPatternList(List<PatternType> patternList) {
  388           for (int i = 0; i < patternList.size(); i++) {
  389           	PatternType pattern = patternList.get(i);
  390               System.out.println("ArtifactID:" + pattern.getArtifactId()
  391                       + " GroupID:" + pattern.getGroupId() + " Module:"
  392                       + pattern.getModule() + " Version:" + pattern.getVersion()
  393                       + " Name:" + pattern.getName());
  394           }
  395       }
  396   
  397       protected void printDeploymentPatternList(
  398               List<org.apache.geronimo.xml.ns.deployment_1.PatternType> patternList) {
  399           for (int i = 0; i < patternList.size(); i++) {
  400           	org.apache.geronimo.xml.ns.deployment_1.PatternType pattern = patternList
  401                       .get(i);
  402               System.out.println("ArtifactID:" + pattern.getArtifactId()
  403                       + " GroupID:" + pattern.getGroupId() + " Module:"
  404                       + pattern.getVersion() + " Version:" + pattern.getType()
  405                       + " Name:" + pattern.getCustomFoo());
  406           }
  407       }
  408   
  409       protected void printDependencies(List<DependencyType> dependencyList) {
  410           for (int i = 0; i < dependencyList.size(); i++) {
  411           	DependencyType dependency = dependencyList.get(i);
  412               System.out.println("ArtifactID:" + dependency.getArtifactId()
  413                       + " GroupID:" + dependency.getGroupId() + " Type:"
  414                       + dependency.getType() + " Version:"
  415                       + dependency.getVersion());
  416           }
  417       }
  418   
  419       public void printServerInfo() {
  420           System.out.println("EJB Modules: \n");
  421           List<PatternType> ejbModules = getDeployedEjbs();
  422           printNamingPatternList(ejbModules);
  423           System.out.println("\n\nSecurity Realms: \n");
  424           List<String> securityRealms = getSecurityRealms();
  425           System.out.println(securityRealms.toString());
  426           System.out.println("\n\nJMS Connection Factories: \n");
  427           List<PatternType> jmsConnectionFactories = getJmsConnectionFactories();
  428           printNamingPatternList(jmsConnectionFactories);
  429           System.out.println("\n\nJMS Destinations: \n");
  430           List<PatternType> jmsDestinations = getJmsDestinations();
  431           printNamingPatternList(jmsDestinations);
  432           System.out.println("\n\nJDBC Connection Pools: \n");
  433           List<PatternType> jdbcConnectionPools = getJdbcConnectionPools();
  434           printNamingPatternList(jdbcConnectionPools);
  435           System.out.println("\n\nJava Mail Resources: \n");
  436           List<PatternType> javaMailResources = getJavaMailSessions();
  437           printNamingPatternList(javaMailResources);
  438           System.out.println("\n\nCredential Stores: \n");
  439           List<org.apache.geronimo.xml.ns.deployment_1.PatternType> credentialStores = getDeployedCredentialStores();
  440           printDeploymentPatternList(credentialStores);
  441           System.out.println("\n\nCommon Libs: \n");
  442           List<DependencyType> dependencies = getCommonLibs();
  443           printDependencies(dependencies);
  444       }
  445   
  446   }

Home » geronimo-eclipse-plugin-2.2-source-release » org.apache.geronimo.st.v11.core » [javadoc | source]