001: /**
002: * Copyright 2004 Sun Microsystems, Inc. All
003: * rights reserved. Use of this product is subject
004: * to license terms. Federal Acquisitions:
005: * Commercial Software -- Government Users
006: * Subject to Standard License Terms and
007: * Conditions.
008: *
009: * Sun, Sun Microsystems, the Sun logo, and Sun ONE
010: * are trademarks or registered trademarks of Sun Microsystems,
011: * Inc. in the United States and other countries.
012: */package com.sun.portal.search.admin.mbeans;
013:
014: import java.io.*;
015: import java.util.*;
016: import java.util.logging.*;
017: import java.net.*;
018: import java.lang.*;
019: import java.text.*;
020: import java.net.InetAddress;
021:
022: import javax.management.MBeanServer;
023: import javax.management.MBeanServerConnection;
024: import javax.management.ObjectName;
025: import javax.management.remote.JMXConnector;
026:
027: import com.sun.portal.log.common.*;
028:
029: import com.sun.portal.admin.server.PASModule;
030: import com.sun.portal.admin.server.mbeans.PSResource;
031: import com.sun.portal.admin.common.context.PSConfigContext;
032: import com.sun.portal.admin.common.context.PortalDomainContext;
033: import com.sun.portal.admin.common.util.AdminUtil;
034: import com.sun.portal.admin.common.PSMBeanException;
035: import com.sun.portal.admin.server.AdminServerUtil;
036: import com.sun.portal.log.common.PortalLogger;
037: import com.sun.portal.search.robot.*;
038: import com.sun.portal.search.admin.mbeans.tasks.FilterSimulator;
039:
040: public class SiteProbe extends PSResource implements SiteProbeMBean {
041:
042: private ObjectName objectName = null;
043: private String host = null;
044: public String serverRoot = null;
045: public String configDir = null;
046: public RobotConfig robotConfig = null;
047:
048: private static Logger logger = PortalLogger
049: .getLogger(SiteProbe.class);
050:
051: static final String SEARCH_CONF = "search.conf";
052: static final String FILTER_CONF = "filterrules.conf";
053:
054: public void init(PSConfigContext cc, PortalDomainContext pdc,
055: List path) {
056: super .init(cc, pdc, path);
057:
058: try {
059: objectName = AdminUtil.getResourceMBeanObjectName(
060: AdminUtil.SEARCH_SITEPROBE_MBEAN_TYPE, path);
061: host = pdc
062: .getAttributeValue(
063: AdminUtil.SEARCH_SITEPROBE_MBEAN_TYPE,
064: path, "Host");
065: } catch (Exception e) {
066: logger.log(Level.SEVERE, "PSSH_CSPSAMB0003", e);
067: }
068:
069: try {
070: if (AdminUtil.isLocal(host)) {
071: initConfig(path);
072: }
073: } catch (Exception e) {
074: //logger.logp(Level.SEVERE, "SiteProbe", "init()", "initialization failed", e);
075: logger.log(Level.SEVERE, "PSSH_CSPSAMB0003", e);
076: }
077:
078: }
079:
080: public void initConfig(List path) throws PSMBeanException {
081:
082: try {
083: //get the SearchServer associated with the SiteProbe MBean
084: MBeanServer ms = PASModule.getMBeanServer();
085: LinkedList serverpath = new LinkedList();
086: serverpath.addFirst(path.get(2));
087: serverpath.addFirst(path.get(1));
088: ObjectName searchServer = AdminUtil
089: .getResourceMBeanObjectName(
090: AdminUtil.SEARCHSERVER_MBEAN_TYPE,
091: serverpath);
092:
093: serverRoot = (String) ms.getAttribute(searchServer,
094: "SearchServerRoot");
095:
096: configDir = (String) ms.getAttribute(searchServer,
097: "ConfigDir");
098:
099: if (serverRoot == null || configDir == null) {
100: throw new PSMBeanException(
101: "SiteProbe.initConfig(): initialization failed with null server root or config directory");
102: }
103:
104: } catch (Exception e) {
105: logger.log(Level.SEVERE, "PSSH_CSPSAMB0003", e);
106: throw new PSMBeanException("SiteProbe.initConfig()", e
107: .toString(), e);
108: }
109:
110: }
111:
112: public HashMap runSiteProbe(String url, Boolean showDNS)
113: throws PSMBeanException, java.net.UnknownHostException {
114:
115: if (!AdminUtil.isLocal(host)) {
116: try {
117: JMXConnector jmxc = AdminServerUtil
118: .getJMXConnector(host);
119: MBeanServerConnection msc = jmxc
120: .getMBeanServerConnection();
121: Object[] params = { url, showDNS };
122: String[] signatures = { "java.lang.String",
123: "java.lang.Boolean" };
124: HashMap result = (HashMap) msc.invoke(objectName,
125: "runSiteProbe", params, signatures);
126: jmxc.close();
127: return result;
128: } catch (Exception e) {
129: logger.log(Level.SEVERE, "PSSH_CSPSAMB0003", e);
130: throw new PSMBeanException("SiteProbe:runSiteProbe", e
131: .toString(), e);
132: }
133: } else {
134:
135: HashMap result_map = null;
136:
137: boolean showdns = showDNS.booleanValue();
138:
139: FilterSimulator frsim = new FilterSimulator();
140:
141: robotConfig = new RobotConfig(configDir);
142:
143: if (robotConfig == null) {
144: throw new PSMBeanException(
145: "SiteProbe.runSiteProbe(): cannot get robot configurations");
146: }
147:
148: String urlstring = getValidatedURLString(url);
149:
150: if (urlstring == null) {
151: throw new PSMBeanException(
152: "SiteProbe.runSiteProbe(): cannot validate the url "
153: + url.toString());
154: }
155:
156: ProcessConfig pconf = robotConfig.processConf;
157: String shh = (String) pconf.get("smart-host-heuristics");
158: boolean is_shh;
159: if (shh.equalsIgnoreCase("true"))
160: is_shh = true;
161: else
162: is_shh = false;
163:
164: try {
165: result_map = frsim.runProb(is_shh, serverRoot, showdns,
166: urlstring);
167: } catch (Exception e) {
168: logger.log(Level.SEVERE, "PSSH_CSPSAMB0003", e);
169: throw new PSMBeanException("SiteProbe.runSiteProbe()",
170: e.toString(), e);
171: }
172:
173: if (result_map != null) {
174: result_map.put("smart-host-heuristics", shh);
175: }
176:
177: return result_map;
178: }
179:
180: }
181:
182: public String getValidatedURLString(String sitename) {
183: URL url = null;
184:
185: sitename = sitename.replace('\\', '/');
186:
187: String urlstring = sitename;
188:
189: int i = sitename.indexOf("://");
190: if (i < 0) {
191: if (sitename.startsWith("file:")) {
192: int j = sitename.indexOf(":/");
193: urlstring = "file://localhost"
194: + sitename.substring(j + 1);
195: } else if (sitename.startsWith("//")) {
196: urlstring = "file:" + sitename;
197: } else if (sitename.startsWith("/")) {
198: urlstring = "file://localhost" + sitename;
199: } else {
200: urlstring = "http://" + sitename;
201: }
202: }
203:
204: if (sitename.startsWith("file:///")) {
205: int k = sitename.indexOf(":///");
206: urlstring = "file://localhost" + sitename.substring(k + 3);
207: }
208:
209: try {
210: url = new URL(urlstring);
211: int port = url.getPort();
212: String host = url.getHost();
213: String path = url.getPath();
214: if (port < 0 && !url.getProtocol().equalsIgnoreCase("file")) {
215: port = url.getDefaultPort();
216: }
217: if (path == null || path.length() == 0) {
218: path = "/";
219: }
220: if (host == null || host.length() == 0) {
221: host = "localhost";
222: }
223:
224: if (!(url.getProtocol()).equals("file"))
225: url = new URL(url.getProtocol(), host, port, path);
226: } catch (java.net.MalformedURLException e) {
227: logger.log(Level.SEVERE, "PSSH_CSPSAMB0003", e);
228: return null;
229: }
230:
231: return url.toString();
232: }
233:
234: }
|