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.net.URI;
   24   import java.net.URISyntaxException;
   25   import java.util.Map;
   26   
   27   import org.apache.geronimo.farm.config.NodeInfo;
   28   import org.apache.geronimo.farm.discovery.DiscoveryAgent;
   29   import org.apache.geronimo.farm.discovery.DiscoveryListener;
   30   import org.apache.geronimo.gbean.annotation.GBean;
   31   import org.apache.geronimo.gbean.annotation.ParamAttribute;
   32   import org.apache.geronimo.gbean.annotation.ParamReference;
   33   import org.apache.geronimo.system.plugin.DownloadResults;
   34   import org.slf4j.Logger;
   35   import org.slf4j.LoggerFactory;
   36   
   37   /**
   38    * @version $Rev: 720842 $ $Date: 2008-11-26 05:18:49 -0800 (Wed, 26 Nov 2008) $
   39    */
   40   @GBean
   41   public class FarmDiscoveryListener implements DiscoveryListener {
   42   
   43       private static final Logger log = LoggerFactory.getLogger(FarmDiscoveryListener.class);
   44       private DiscoveryAgent discoveryAgent;
   45       private NodeListener nodeListener;
   46       private String defaultClusterName;
   47       private String discoveryType;
   48       private String userName;
   49       private String password;
   50   
   51   
   52       public FarmDiscoveryListener(@ParamAttribute(name = "defaultClusterName")String defaultClusterName,
   53                                    @ParamAttribute(name = "discoveryType")String discoveryType,
   54                                    @ParamAttribute(name = "userName")String userName,
   55                                    @ParamAttribute(name = "password")String password,
   56                                    @ParamReference(name = "DiscoveryAgent")DiscoveryAgent discoveryAgent,
   57                                    @ParamReference(name = "NodeListener")NodeListener nodeListener) {
   58           discoveryAgent.setDiscoveryListener(this);
   59           this.discoveryAgent = discoveryAgent;
   60           this.nodeListener = nodeListener;
   61           this.defaultClusterName = defaultClusterName;
   62           this.discoveryType = discoveryType;
   63           this.userName = userName;
   64           this.password = password;
   65       }
   66   
   67       /**
   68        * Starts the GBean.  This informs the GBean that it is about to transition to the running state.
   69        *
   70        * @throws Exception if the target failed to start; this will cause a transition to the failed state
   71        */
   72       public void doStart() throws Exception {
   73       }
   74   
   75       /**
   76        * Stops the target.  This informs the GBean that it is about to transition to the stopped state.
   77        *
   78        * @throws Exception if the target failed to stop; this will cause a transition to the failed state
   79        */
   80       public void doStop() throws Exception {
   81           doFail();
   82       }
   83   
   84       /**
   85        * Fails the GBean.  This informs the GBean that it is about to transition to the failed state.
   86        */
   87       public void doFail() {
   88           discoveryAgent.setDiscoveryListener(null);
   89       }
   90   
   91       public void serviceAdded(URI service) {
   92           NodeInfo nodeInfo = toNodeInfo(service);
   93           if (nodeInfo != null) {
   94               String clusterName = toClusterName(service);
   95               Map<String, DownloadResults> results = nodeListener.addNode(clusterName, nodeInfo);
   96               log(nodeInfo, results);
   97           }
   98       }
   99   
  100       public void serviceRemoved(URI service) {
  101           NodeInfo nodeInfo = toNodeInfo(service);
  102           if (nodeInfo != null) {
  103               String clusterName = toClusterName(service);
  104               nodeListener.removeNode(clusterName, nodeInfo.getName());
  105           }
  106       }
  107   
  108       NodeInfo toNodeInfo(URI service) {
  109           String typeScheme = service.getScheme();
  110           if (!discoveryType.equals(typeScheme)) {
  111               return null;
  112           }
  113           try {
  114               URI jmxService = new URI(service.getSchemeSpecificPart());
  115               JpaNodeInfo nodeInfo = new JpaNodeInfo();
  116               String host = jmxService.getHost();
  117               int port = jmxService.getPort();
  118               String urlPath = jmxService.getPath().substring(1);
  119               //TODO not sure about this one
  120               String protocol = jmxService.getScheme();
  121               String name = getValue("node", jmxService.getQuery(), host + ":" + port);
  122               nodeInfo.setName(name);
  123               nodeInfo.setConnectorInfo(userName, password, protocol, host, port, urlPath, false);
  124               return nodeInfo;
  125           } catch (URISyntaxException e) {
  126               //TODO log error
  127               return null;
  128           }
  129       }
  130   
  131       String toClusterName(URI service) {
  132           String typeScheme = service.getScheme();
  133           if (!discoveryType.equals(typeScheme)) {
  134               return null;
  135           }
  136           try {
  137               URI jmxService = new URI(service.getSchemeSpecificPart());
  138               return getValue("cluster", jmxService.getQuery(), defaultClusterName);
  139           } catch (URISyntaxException e) {
  140               //TODO log error
  141               return null;
  142           }
  143       }
  144   
  145       private String getValue(String key, String query, String defaultValue) {
  146           String[] bits = query.split("[&=]");
  147           if (bits.length % 2 != 0) {
  148               throw new IllegalArgumentException("Can't parse query string: " + query);
  149           }
  150           for (int i = 0; i < bits.length; i = i + 2) {
  151               if (key.equals(bits[i])) {
  152                   return bits[i + 1];
  153               }
  154           }
  155           return defaultValue;
  156       }
  157   
  158       private void log(NodeInfo nodeInfo, Map<String, DownloadResults> results) {
  159           log.info("installing to node: " + nodeInfo.getName());
  160           for (Map.Entry<String, DownloadResults> entry: results.entrySet()) {
  161               log.info("installation results for plugin list: " + entry.getKey());
  162               DownloadResults downloadResults = entry.getValue();
  163               log.info("installed: " + downloadResults.getInstalledConfigIDs());
  164               if (downloadResults.isFailed()) {
  165                   log.info("failure: ", downloadResults.getFailure());
  166               }
  167           }
  168   
  169       }
  170   
  171   }

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