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

Save This Page
Home » geronimo-eclipse-plugin-2.2-source-release » org.apache.geronimo.st.v21.core » [javadoc | source]