Home » geronimo-2.2-source-release » org.apache.geronimo.farm.plugin » [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.farm.plugin;
   18   
   19   
   20   import org.apache.geronimo.gbean.annotation.GBean;
   21   import org.apache.geronimo.gbean.annotation.ParamReference;
   22   import org.apache.geronimo.farm.discovery.MulticastLocation;
   23   import org.apache.geronimo.farm.discovery.MulticastSearch;
   24   
   25   import java.io.InputStream;
   26   import java.io.OutputStream;
   27   import java.io.IOException;
   28   import java.io.PrintStream;
   29   import java.util.regex.Pattern;
   30   import java.util.regex.Matcher;
   31   import java.util.concurrent.atomic.AtomicBoolean;
   32   import java.net.URI;
   33   
   34   @GBean
   35   public class HeartbeatMonitor implements org.apache.geronimo.system.plugin.HeartbeatMonitor {
   36   
   37       private final MulticastLocation location;
   38   
   39       public HeartbeatMonitor(@ParamReference(name = "MulticastLocation") MulticastLocation location) {
   40           this.location = location;
   41       }
   42   
   43       public void monitor(final InputStream in, final OutputStream output, String pattern) {
   44           if (pattern == null || pattern.equals("")) pattern = ".*";
   45   
   46           final PrintStream out = new PrintStream(output);
   47   
   48           final Pattern regex;
   49           try {
   50               regex = Pattern.compile(pattern);
   51           } catch (Exception e) {
   52               out.println("Invalid java.util.regex.Pattern \"" + pattern + "\"");
   53               return;
   54           }
   55   
   56           out.println("Hit any key to stop.");
   57   
   58           final AtomicBoolean stop = new AtomicBoolean();
   59   
   60           Runnable runnable = new Runnable(){
   61               public void run() {
   62                   try {
   63                       in.read();
   64                   } catch (IOException e) {
   65                   } finally {
   66                       stop.set(true);
   67                   }
   68               }
   69           };
   70           Thread thread = new Thread(runnable);
   71           thread.setDaemon(true);
   72           thread.start();
   73   
   74           try {
   75               MulticastSearch multicast = new MulticastSearch(location.getHost(), location.getPort());
   76   
   77   
   78               multicast.search(new MulticastSearch.Filter(){
   79                   public boolean accept(URI service) {
   80                       String s = service.toString();
   81                       Matcher matcher = regex.matcher(s);
   82                       if (matcher.matches()){
   83                           out.println(s);
   84                       }
   85                       return stop.get();
   86                   }
   87               });
   88           } catch (IOException e) {
   89               e.printStackTrace(out);
   90           }
   91       }
   92   }

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