Home » geronimo-2.2-source-release » org.apache.geronimo.farm.plugin » [javadoc | source]

    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one
    3    * or more contributor license agreements.  See the NOTICE file
    4    * distributed with this work for additional information
    5    * regarding copyright ownership.  The ASF licenses this file
    6    * to you under the Apache License, Version 2.0 (the
    7    * "License"); you may not use this file except in compliance
    8    * with the License.  You may obtain a copy of the License at
    9    *
   10    *  http://www.apache.org/licenses/LICENSE-2.0
   11    *
   12    * Unless required by applicable law or agreed to in writing,
   13    * software distributed under the License is distributed on an
   14    * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
   15    * KIND, either express or implied.  See the License for the
   16    * specific language governing permissions and limitations
   17    * under the License.
   18    */
   19   
   20   
   21   package org.apache.geronimo.farm.plugin;
   22   
   23   import java.io.IOException;
   24   import java.util.Collections;
   25   import java.util.HashMap;
   26   import java.util.List;
   27   import java.util.Map;
   28   
   29   import javax.persistence.EntityManager;
   30   import javax.persistence.EntityManagerFactory;
   31   import javax.persistence.EntityTransaction;
   32   import javax.persistence.NoResultException;
   33   import javax.persistence.Query;
   34   
   35   import org.apache.geronimo.farm.config.NodeInfo;
   36   import org.apache.geronimo.gbean.annotation.GBean;
   37   import org.apache.geronimo.gbean.annotation.ParamAttribute;
   38   import org.apache.geronimo.gbean.annotation.ParamReference;
   39   import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory;
   40   import org.apache.geronimo.kernel.config.ConfigurationManager;
   41   import org.apache.geronimo.kernel.config.NoSuchConfigException;
   42   import org.apache.geronimo.kernel.repository.Artifact;
   43   import org.apache.geronimo.persistence.PersistenceUnitGBean;
   44   import org.apache.geronimo.system.plugin.DownloadResults;
   45   import org.apache.geronimo.system.plugin.PluginInstaller;
   46   import org.apache.geronimo.system.plugin.model.PluginListType;
   47   import org.slf4j.Logger;
   48   import org.slf4j.LoggerFactory;
   49   
   50   /**
   51    * //need interface for operations
   52    * // need gshell commands
   53    *
   54    * @version $Rev: 804897 $ $Date: 2009-08-17 01:23:27 -0700 (Mon, 17 Aug 2009) $
   55    */
   56   @GBean
   57   public class FarmGBean implements NodeListener, org.apache.geronimo.system.plugin.Farm {
   58       private static final Logger log = LoggerFactory.getLogger(FarmGBean.class);
   59       private String defaultRepository;
   60       private EntityManagerFactory emf;
   61   
   62       public FarmGBean(String defaultPluginRepository, EntityManagerFactory emf) {
   63           this.defaultRepository = defaultPluginRepository;
   64           this.emf = emf;
   65       }
   66   
   67       public FarmGBean(@ParamAttribute(name = "defaultPluginRepository")String defaultPluginRepository,
   68                        @ParamReference(name = "PersistenceUnit", namingType = NameFactory.PERSISTENCE_UNIT)PersistenceUnitGBean persistenceUnitGBean) {
   69           this.defaultRepository = defaultPluginRepository;
   70           this.emf = persistenceUnitGBean.getEntityManagerFactory();
   71       }
   72   
   73   
   74       public Map<String, DownloadResults> addNode(String clusterName, NodeInfo nodeInfo) {
   75           log.info("Node "  + nodeInfo.getName() + " added to cluster " + clusterName);
   76           JpaClusterInfo cluster;
   77           JpaNodeInfo jpaNodeInfo;
   78           synchronized (this) {
   79               JpaContext clusterContext = new JpaContext(emf);
   80               cluster = clusterContext.getClusterInfo(clusterName);
   81               jpaNodeInfo = clusterContext.getNodeInfo(cluster, nodeInfo);
   82               clusterContext.close();
   83           }
   84           Map<String, DownloadResults> installedPluginLists = new HashMap<String, DownloadResults>();
   85           for (JpaPluginList pluginList : cluster.getPluginLists()) {
   86               DownloadResults downloadResults = installToNode(pluginList, jpaNodeInfo);
   87               installedPluginLists.put(pluginList.getName(), downloadResults);
   88           }
   89           return installedPluginLists;
   90       }
   91   
   92       public void removeNode(String clusterName, String nodeName) {
   93           log.info("Node "  + nodeName + " renoved from cluster " + clusterName);
   94           synchronized (this) {
   95               JpaContext clusterContext = new JpaContext(emf);
   96               JpaClusterInfo cluster = clusterContext.getClusterInfo(clusterName);
   97               for (JpaNodeInfo nodeInfo: cluster.getJpaNodeInfos()) {
   98                   if (nodeName.equals(nodeInfo.getName())) {
   99                       cluster.getJpaNodeInfos().remove(nodeInfo);
  100                       break;
  101                   }
  102               }
  103               clusterContext.close();
  104           }
  105       }
  106   
  107       public Map<String, DownloadResults> addPluginList(String clusterName, JpaPluginList pluginList) {
  108           JpaClusterInfo cluster;
  109           synchronized (this) {
  110               JpaContext clusterContext = new JpaContext(emf);
  111               cluster = clusterContext.getClusterInfo(clusterName);
  112               cluster.getPluginLists().add(pluginList);
  113               clusterContext.close();
  114           }
  115           return installToCluster(pluginList, cluster);
  116       }
  117   
  118       public Map<String, DownloadResults> addPluginList(String clusterName, String pluginListName) {
  119           JpaClusterInfo cluster;
  120           JpaPluginList pluginList;
  121           synchronized (this) {
  122               JpaContext clusterContext = new JpaContext(emf);
  123               cluster = clusterContext.getClusterInfo(clusterName);
  124               pluginList = clusterContext.getPluginList(pluginListName, defaultRepository);
  125               cluster.getPluginLists().add(pluginList);
  126               clusterContext.close();
  127           }
  128           return installToCluster(pluginList, cluster);
  129       }
  130   
  131       public Map<String, DownloadResults> addPlugin(String pluginListName, String artifactURI) {
  132           return addPlugin(pluginListName, new JpaPluginInstance(artifactURI));
  133       }
  134   
  135       public Map<String, DownloadResults> addPlugin(String pluginListName, JpaPluginInstance pluginInstance) {
  136           JpaPluginList pluginList;
  137           synchronized (this) {
  138               JpaContext pluginListContext = new JpaContext(emf);
  139               pluginList = pluginListContext.getPluginList(pluginListName, defaultRepository);
  140               pluginList.getPlugins().add(pluginInstance);
  141               pluginListContext.close();
  142           }
  143           return installToClusters(pluginList);
  144       }
  145   
  146       public Map<String, DownloadResults> removePluginFromPluginList(String pluginListName, String artifactURI) {
  147           return removePluginFromPluginList(pluginListName, new JpaPluginInstance(artifactURI));
  148       }
  149   
  150       public Map<String, DownloadResults> removePluginFromPluginList(String pluginListName, JpaPluginInstance pluginInstance) {
  151           JpaPluginList pluginList;
  152           synchronized (this) {
  153               JpaContext pluginListContext = new JpaContext(emf);
  154               pluginList = pluginListContext.getPluginList(pluginListName, defaultRepository);
  155               pluginList.getPlugins().remove(pluginInstance);
  156               pluginListContext.close();
  157           }
  158           return removeFromClusters(pluginList, pluginInstance);
  159       }
  160   
  161       public Map<String, DownloadResults> removePluginListFromCluster(String clusterName, String pluginListName) {
  162           JpaClusterInfo cluster;
  163           JpaPluginList pluginList;
  164           synchronized (this) {
  165               JpaContext clusterContext = new JpaContext(emf);
  166               cluster = clusterContext.getClusterInfo(clusterName);
  167               pluginList = clusterContext.getPluginList(pluginListName, defaultRepository);
  168               cluster.getPluginLists().remove(pluginList);
  169               clusterContext.close();
  170           }
  171           return removeFromCluster(pluginList.getPlugins(), cluster);
  172       }
  173   
  174       public Map<String, DownloadResults> addPluginToCluster(String clusterName, String pluginListName, String artifactURI) {
  175           return addPluginToCluster(clusterName, pluginListName, new JpaPluginInstance(artifactURI));
  176       }
  177   
  178       public Map<String, DownloadResults> addPluginToCluster(String clusterName, String pluginListName, JpaPluginInstance pluginInstance) {
  179           JpaPluginList pluginList;
  180           synchronized (this) {
  181               JpaContext clusterContext = new JpaContext(emf);
  182               JpaClusterInfo cluster = clusterContext.getClusterInfo(clusterName);
  183               pluginList = getPluginList(cluster, pluginListName);
  184               pluginList.getPlugins().add(pluginInstance);
  185               clusterContext.close();
  186           }
  187           return installToClusters(pluginList);
  188       }
  189   
  190       private Map<String, DownloadResults> installToClusters(JpaPluginList pluginList) {
  191           Map<String, DownloadResults> results = new HashMap<String, DownloadResults>();
  192           for (JpaClusterInfo cluster : pluginList.getClusters()) {
  193               results.putAll(installToCluster(pluginList, cluster));
  194           }
  195           return results;
  196       }
  197   
  198       private Map<String, DownloadResults> installToCluster(JpaPluginList pluginList, JpaClusterInfo cluster) {
  199           Map<String, DownloadResults> installedNodes = new HashMap<String, DownloadResults>();
  200           for (JpaNodeInfo jpaNodeInfo : cluster.getJpaNodeInfos()) {
  201               DownloadResults downloadResults = installToNode(pluginList, jpaNodeInfo);
  202               installedNodes.put(jpaNodeInfo.getName(), downloadResults);
  203           }
  204           return installedNodes;
  205       }
  206   
  207       private DownloadResults installToNode(JpaPluginList jpaPluginList, JpaNodeInfo jpaNodeInfo) {
  208           try {
  209               PluginInstaller pluginInstaller = jpaNodeInfo.getPluginInstaller();
  210               PluginListType pluginList = jpaPluginList.getPluginList();
  211               //TODO parameterize restrictToDefaultRepository
  212               return pluginInstaller.install(pluginList, defaultRepository, false, null, null);
  213           } catch (IOException e) {
  214               DownloadResults downloadResults = new DownloadResults();
  215               downloadResults.setFailure(e);
  216               return downloadResults;
  217           }
  218       }
  219   
  220       private Map<String, DownloadResults> removeFromClusters(JpaPluginList clusterPluginList, JpaPluginInstance pluginInstance) {
  221           List<JpaPluginInstance> pluginList = pluginInstance == null? clusterPluginList.getPlugins(): Collections.singletonList(pluginInstance);
  222           Map<String, DownloadResults> results = new HashMap<String, DownloadResults>();
  223           for (JpaClusterInfo cluster : clusterPluginList.getClusters()) {
  224               results.putAll(removeFromCluster(pluginList, cluster));
  225           }
  226           return results;
  227       }
  228   
  229       private Map<String, DownloadResults> removeFromCluster(List<JpaPluginInstance> pluginList, JpaClusterInfo cluster) {
  230           Map<String, DownloadResults> installedNodes = new HashMap<String, DownloadResults>();
  231           for (JpaNodeInfo jpaNodeInfo : cluster.getJpaNodeInfos()) {
  232               DownloadResults downloadResults = removeFromNode(pluginList, jpaNodeInfo);
  233               installedNodes.put(jpaNodeInfo.getName(), downloadResults);
  234           }
  235           return installedNodes;
  236       }
  237   
  238       private DownloadResults removeFromNode(List<JpaPluginInstance> pluginList, JpaNodeInfo jpaNodeInfo) {
  239           DownloadResults downloadResults = new DownloadResults();
  240           try {
  241               ConfigurationManager configurationManager = jpaNodeInfo.getConfigurationManager();
  242               for (JpaPluginInstance jpaPluginInstance: pluginList) {
  243                   Artifact artifact = jpaPluginInstance.toArtifact();
  244                   configurationManager.uninstallConfiguration(artifact);
  245                   downloadResults.addRemovedConfigID(artifact);
  246               }
  247           } catch (IOException e) {
  248               downloadResults.setFailure(e);
  249           } catch (NoSuchConfigException e) {
  250               downloadResults.setFailure(e);
  251           }
  252           return downloadResults;
  253       }
  254   
  255   
  256       private JpaPluginList getPluginList(JpaClusterInfo cluster, String pluginListName) {
  257           for (JpaPluginList pluginList : cluster.getPluginLists()) {
  258               if (pluginList.getName().equals(pluginListName)) {
  259                   return pluginList;
  260               }
  261           }
  262           JpaPluginList pluginList = new JpaPluginList();
  263           pluginList.setName(pluginListName);
  264           pluginList.setDefaultPluginRepository(defaultRepository);
  265           cluster.getPluginLists().add(pluginList);
  266           return pluginList;
  267       }
  268   
  269   
  270       public static class JpaContext {
  271           private EntityManager em;
  272           private EntityTransaction entityTransaction;
  273   
  274           public JpaContext(EntityManagerFactory emf) {
  275               em = emf.createEntityManager();
  276               entityTransaction = em.getTransaction();
  277               entityTransaction.begin();
  278           }
  279   
  280           public JpaClusterInfo getClusterInfo(String clusterName) {
  281               Query query = em.createNamedQuery("clusterByName");
  282               query.setParameter("name", clusterName);
  283               try {
  284                   return (JpaClusterInfo) query.getSingleResult();
  285               } catch (NoResultException e) {
  286                   JpaClusterInfo clusterInfo = new JpaClusterInfo();
  287                   clusterInfo.setName(clusterName);
  288                   em.persist(clusterInfo);
  289                   em.flush();
  290                   return clusterInfo;
  291               }
  292           }
  293   
  294           public JpaNodeInfo getNodeInfo(JpaClusterInfo cluster, NodeInfo nodeInfo) {
  295               Query query = em.createNamedQuery("nodeByName");
  296               query.setParameter("name", nodeInfo.getName());
  297               JpaNodeInfo jpaNodeInfo;
  298               try {
  299                   jpaNodeInfo = (JpaNodeInfo) query.getSingleResult();
  300               } catch (NoResultException e) {
  301                    jpaNodeInfo = new JpaNodeInfo(nodeInfo);
  302                   em.persist(jpaNodeInfo);
  303               }
  304               if (jpaNodeInfo.getCluster() == null)  {
  305                   jpaNodeInfo.setCluster(cluster);
  306               } else if (!jpaNodeInfo.getCluster().getName().equals(cluster.getName())){
  307                   throw new IllegalStateException("cannot move node to another cluster");
  308               }
  309               em.flush();
  310               return jpaNodeInfo;
  311           }
  312   
  313           public JpaPluginList getPluginList(String name, String defaultRepository) {
  314               Query query = em.createNamedQuery("pluginListByName");
  315               query.setParameter("name", name);
  316               try {
  317                   return (JpaPluginList) query.getSingleResult();
  318               } catch (NoResultException e) {
  319                   JpaPluginList pluginList = new JpaPluginList();
  320                   pluginList.setName(name);
  321                   pluginList.setDefaultPluginRepository(defaultRepository);
  322                   em.persist(pluginList);
  323                   em.flush();
  324                   return pluginList;
  325               }
  326           }
  327   
  328           public void close() {
  329               entityTransaction.commit();
  330               em.close();
  331           }
  332       }
  333   
  334   }

Home » geronimo-2.2-source-release » org.apache.geronimo.farm.plugin » [javadoc | source]