Source code: org/altara/mars/ProbeFactory.java
1 /* MARS Network Monitoring Engine
2 Copyright (C) 1999 Brian H. Trammell
3 Copyright (C) 2002 Leapfrog Research & Development, LLC
4
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, it is available at
17 http:///www.gnu.org/copyleft/gpl.html, or by writing to the
18 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA.
20 */
21
22 package org.altara.mars;
23
24 import java.util.*;
25 import java.net.*;
26 import java.io.*;
27 import org.altara.util.*;
28 import org.altara.mars.engine.*;
29 import org.apache.oro.text.regex.*;
30
31 /** ProbeFactory creates probes of an appropriate class for a
32 given type of service. ProbeFactory is considered part of the
33 data model, since a Service's ProbeFactory encapsulates
34 how to test the service, and therefore what type of
35 service it is.
36 <p>
37 ProbeFactory itself maps service type names ("http", "smtp", etc.)
38 to ProbeFactory instances, and handles dynamic loading of
39 probes from JAR files in the home directory
40 */
41
42 public abstract class ProbeFactory implements java.io.Serializable {
43
44 public static final String PROBE_JAR_PREFIX =
45 "probe_";
46 public static final String PROBE_MANIFEST_KEY =
47 "MarsProbeFactoryClass";
48
49 private static Map factoryMap = new TreeMap();
50
51 protected static Perl5Compiler recompiler = new Perl5Compiler();
52
53 public static void registerFactory(ProbeFactory fac) {
54 factoryMap.put(fac.getName(),fac);
55 }
56
57 public static Iterator getRegisteredServiceTypes() {
58 return factoryMap.keySet().iterator();
59 }
60
61 public static ProbeFactory getFactory(String name) {
62 return (ProbeFactory)factoryMap.get(name);
63 }
64
65 public static void loadDynamic(File homeDir) {
66 ProbeLoadExceptionHandler pleh = new ProbeLoadExceptionHandler();
67 ProbeFilenameFilter pff = new ProbeFilenameFilter();
68 Main.getMain().showStatus("Scanning for probes in "+homeDir.getAbsolutePath()+"...");
69 Iterator probes = ExtensionLoader.scanExtensions(homeDir,
70 pff, PROBE_MANIFEST_KEY, pleh).iterator();
71 while (probes.hasNext()) {
72 try {
73 Class nextProbeFactoryClass = (Class)probes.next();
74 ProbeFactory nextProbeFactory =
75 (ProbeFactory)nextProbeFactoryClass.newInstance();
76 registerFactory(nextProbeFactory);
77 Main.getMain().showStatus("Loaded probe "+nextProbeFactoryClass);
78 } catch (Exception ex) {
79 pleh.handleLoadException(null,ex);
80 }
81 }
82 }
83
84 private static class ProbeLoadExceptionHandler
85 implements LoadExceptionHandler {
86 public void handleLoadException(File file, Exception ex) {
87 if (file == null) {
88 Main.getMain().showStatus("Error loading probe: "+ex.getMessage());
89 } else {
90 Main.getMain().showStatus("Error loading probe from "+
91 file.getAbsolutePath()+": "+ex.getMessage());
92 }
93 ex.printStackTrace();
94 }
95 }
96
97 private static class ProbeFilenameFilter
98 implements FilenameFilter {
99 public boolean accept(File dir, String name) {
100 return (name.startsWith(PROBE_JAR_PREFIX) && name.endsWith(".jar"));
101 }
102 }
103
104 private String name;
105
106 protected ProbeFactory(String name) {
107 this.name = name;
108 }
109
110 public String getName() {
111 return name;
112 }
113
114 public abstract Probe createProbe(Service service);
115 public abstract int getDefaultPort();
116
117 public String[] getServiceParamNames() {
118 return null;
119 }
120
121 public String[] getServiceParamLabels() {
122 return null;
123 }
124
125 public String getServiceParamDefault(Service service, String name) {
126 return null;
127 }
128 }