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:
018: import javax.management.MBeanServer;
019: import javax.management.MBeanServerConnection;
020: import javax.management.ObjectName;
021: import javax.management.remote.JMXConnector;
022:
023: import com.sun.portal.log.common.PortalLogger;
024:
025: import com.sun.portal.admin.server.PASModule;
026: import com.sun.portal.admin.server.mbeans.PSResource;
027: import com.sun.portal.admin.server.AdminServerUtil;
028: import com.sun.portal.admin.common.context.PSConfigContext;
029: import com.sun.portal.admin.common.context.PortalDomainContext;
030: import com.sun.portal.admin.common.util.AdminUtil;
031: import com.sun.portal.admin.common.PSMBeanException;
032:
033: import com.sun.portal.search.autoclassify.*;
034: import com.sun.portal.util.Platform;
035:
036: public class Autoclassify extends PSResource implements
037: AutoclassifyMBean {
038:
039: private ObjectName objectName = null;
040: private String host = null;
041: private String serverURL = null;
042: private String serverRoot = null;
043: private static Logger logger = PortalLogger
044: .getLogger(Autoclassify.class);
045: private com.sun.portal.search.autoclassify.Autoclassify ac = null;
046:
047: public void init(PSConfigContext cc, PortalDomainContext pdc,
048: List path) {
049:
050: super .init(cc, pdc, path);
051:
052: try {
053: objectName = AdminUtil.getResourceMBeanObjectName(
054: AdminUtil.SEARCH_AUTOCLASSIFY_MBEAN_TYPE, path);
055: host = pdc.getAttributeValue(
056: AdminUtil.SEARCH_AUTOCLASSIFY_MBEAN_TYPE, path,
057: "Host");
058: } catch (Exception e) {
059: logger.log(Level.SEVERE, "PSSH_CSPSAMB0003", e);
060: }
061:
062: try {
063: //get the SearchServer associated with the Autoclassify MBean
064: MBeanServer ms = PASModule.getMBeanServer();
065: LinkedList serverpath = new LinkedList();
066: serverpath.addFirst(path.get(2));
067: serverpath.addFirst(path.get(1));
068: ObjectName searchServer = AdminUtil
069: .getResourceMBeanObjectName(
070: AdminUtil.SEARCHSERVER_MBEAN_TYPE,
071: serverpath);
072:
073: serverRoot = (String) ms.getAttribute(searchServer,
074: "SearchServerRoot");
075: //logger.logp(Level.SEVERE, "Autoclassify", "init()", "serverRoot is " + serverRoot);
076:
077: String searchServerURL = (String) ms.getAttribute(
078: searchServer, "SearchServerURL");
079: serverURL = searchServerURL + "/search";
080:
081: //logger.logp(Level.SEVERE, "Autoclassify", "init()", "serverURL is " + serverURL);
082:
083: //logger.logp(Level.SEVERE, "Autoclassify", "init()", "Autoclassify mbean initialization succeeded");
084:
085: } catch (Exception e) {
086: //logger.logp(Level.SEVERE, "Autoclassify", "init()", "failed to get search server URL or server root information \n" + e.toString(), e);
087: logger.log(Level.SEVERE, "PSSH_CSPSAMB0003", e);
088: }
089:
090: }
091:
092: public HashMap retrieveConfig() throws PSMBeanException,
093: java.net.UnknownHostException {
094: if (!AdminUtil.isLocal(host)) {
095: try {
096: JMXConnector jmxc = AdminServerUtil
097: .getJMXConnector(host);
098: MBeanServerConnection msc = jmxc
099: .getMBeanServerConnection();
100: Object[] params = {};
101: String[] signatures = {};
102: HashMap result = (HashMap) msc.invoke(objectName,
103: "retrieveConfig", params, signatures);
104: jmxc.close();
105: return result;
106: } catch (Exception e) {
107: logger.log(Level.SEVERE, "PSSH_CSPSAMB0003", e);
108: throw new PSMBeanException(
109: "Autoclassify:retrieveConfig", e.toString(), e);
110: }
111: } else {
112: String logPath = null;
113: String dbPath = null;
114: String hashSize = null;
115:
116: try {
117: logPath = getConfigString("logPath");
118: dbPath = getConfigString("dbPath");
119: hashSize = getConfigString("cacheSize");
120:
121: } catch (Exception e) {
122: //logger.logp(Level.SEVERE, "Autoclassify", "getAutoclassConfig()", "failed to get autoclassify configuration \n" + e.toString(), e);
123: logger.log(Level.SEVERE, "PSSH_CSPSAMB0003", e);
124: throw new PSMBeanException(
125: "errorcode.search.autoclassify.config",
126: "Autoclassify.getConfig()", e);
127: }
128:
129: HashMap autoclass = new HashMap();
130:
131: autoclass.put("LogPath", logPath);
132: autoclass.put("DbPath", dbPath);
133: autoclass.put("HashSize", hashSize);
134:
135: return autoclass;
136: }
137: }
138:
139: public Boolean setConfig(String logPath, String dbPath,
140: String cacheSize) throws PSMBeanException,
141: java.net.UnknownHostException {
142: if (!AdminUtil.isLocal(host)) {
143: try {
144: JMXConnector jmxc = AdminServerUtil
145: .getJMXConnector(host);
146: MBeanServerConnection msc = jmxc
147: .getMBeanServerConnection();
148: Object[] params = { logPath, dbPath, cacheSize };
149: String[] signatures = { "java.lang.String",
150: "java.lang.String", "java.lang.String" };
151: Boolean result = (Boolean) msc.invoke(objectName,
152: "setConfig", params, signatures);
153: jmxc.close();
154: return result;
155: } catch (Exception e) {
156: logger.log(Level.SEVERE, "PSSH_CSPSAMB0003", e);
157: throw new PSMBeanException("Autoclassify:setConfig", e
158: .toString(), e);
159: }
160: } else {
161: //logger.logp(Level.SEVERE, "Autoclassify", "setAutoclassConfig()", "get params logpath dbpath cachesize: " + logPath + " " + dbPath + " " + cacheSize);
162:
163: try {
164:
165: //get the SearchServer associated with the Autoclassify Mbean
166: MBeanServer ms = PASModule.getMBeanServer();
167: LinkedList serverpath = new LinkedList();
168: serverpath.addFirst(path.get(2));
169: serverpath.addFirst(path.get(1));
170: ObjectName objName = AdminUtil
171: .getResourceMBeanObjectName(
172: AdminUtil.SEARCHSERVER_MBEAN_TYPE,
173: serverpath);
174:
175: String[] signature = { "java.lang.String",
176: "java.lang.String" };
177:
178: if (logPath != null && !logPath.equals("")) {
179: Object[] params1 = {
180: AutoclassifyConfig.NAME_LOGFILE, logPath };
181: ms.invoke(objName, "setConfigValue", params1,
182: signature);
183: }
184:
185: if (dbPath != null && !dbPath.equals("")) {
186: Object[] params2 = {
187: AutoclassifyConfig.NAME_DBFILE, dbPath };
188: ms.invoke(objName, "setConfigValue", params2,
189: signature);
190: }
191:
192: if (cacheSize != null && !cacheSize.equals("")) {
193: Object[] params3 = {
194: AutoclassifyConfig.NAME_HASHTABLE_SIZE,
195: cacheSize };
196: ms.invoke(objName, "setConfigValue", params3,
197: signature);
198: }
199:
200: } catch (Exception e) {
201: //logger.logp(Level.SEVERE, "Autoclassify", "setAutoclassConfig()", "failed to set autoclassify configuration \n" + e.toString(), e);
202: logger.log(Level.SEVERE, "PSSH_CSPSAMB0003", e);
203: throw new PSMBeanException(
204: "errorcode.search.autoclassify.config",
205: "Autoclassify.setConfig()", e);
206: }
207:
208: return Boolean.TRUE;
209: }
210: }
211:
212: public String runAutoclassify(ArrayList dbs, String localeStr,
213: String startDate) throws PSMBeanException,
214: java.net.UnknownHostException {
215: if (!AdminUtil.isLocal(host)) {
216: try {
217: JMXConnector jmxc = AdminServerUtil
218: .getJMXConnector(host);
219: MBeanServerConnection msc = jmxc
220: .getMBeanServerConnection();
221: Object[] params = { dbs, localeStr, startDate };
222: String[] signatures = { "java.util.ArrayList",
223: "java.lang.String", "java.lang.String" };
224: String result = (String) msc.invoke(objectName,
225: "runAutoclassify", params, signatures);
226: jmxc.close();
227: return result;
228: } catch (Exception e) {
229: logger.log(Level.SEVERE, "PSSH_CSPSAMB0003", e);
230: throw new PSMBeanException(
231: "Autoclassify:runAutoclassify", e.toString(), e);
232: }
233: } else {
234: StringBuffer sb = new StringBuffer();
235: if (dbs != null) {
236: for (int i = 0; i < dbs.size(); i++) {
237: String name = (String) dbs.get(i);
238: sb.append(runAutoclassify(name, localeStr,
239: startDate));
240: sb.append("\n");
241: }
242: }
243: return sb.toString();
244: }
245: }
246:
247: public String runAutoclassify(String dbname, String localeStr,
248: String startDate) throws PSMBeanException,
249: java.net.UnknownHostException {
250: if (!AdminUtil.isLocal(host)) {
251: try {
252: JMXConnector jmxc = AdminServerUtil
253: .getJMXConnector(host);
254: MBeanServerConnection msc = jmxc
255: .getMBeanServerConnection();
256: Object[] params = { dbname, localeStr, startDate };
257: String[] signatures = { "java.lang.String",
258: "java.lang.String", "java.lang.String" };
259: String result = (String) msc.invoke(objectName,
260: "runAutoclassify", params, signatures);
261: jmxc.close();
262: return result;
263: } catch (Exception e) {
264: logger.log(Level.SEVERE, "PSSH_CSPSAMB0003", e);
265: throw new PSMBeanException(
266: "Autoclassify:runAutoclassify", e.toString(), e);
267: }
268: } else {
269: //logger.logp(Level.SEVERE, "Autoclassify", "runAutoclassify()", "dbname locale startdate are: " + dbname + " " + localeStr + " " + startDate + "\n");
270:
271: String result = null;
272:
273: if (localeStr == null || localeStr.equals(""))
274: localeStr = "en_US";
275:
276: if (serverRoot == null || serverURL == null) {
277: //logger.logp(Level.SEVERE, "Autoclassify", "runAutoclassify()", "cannot run autoclassify because serverRoot or serverURL is null.\n");
278: Object tokens[] = { "Autoclassify.runAutoclassify(): cannot run autoclassify because serverRoot or serverURL is null." };
279: logger.log(Level.SEVERE, "PSSH_CSPSAMB0002", tokens);
280: return "";
281: }
282: String osName = System.getProperty("os.name");
283: if (osName.startsWith("Windows")) {
284: // create the bat file if needed. Only for 7.1u1 patch
285: String psBaseDir = cc.getPSBaseDir().replace('/',
286: File.separatorChar);
287: String batchName = psBaseDir + File.separator + "lib"
288: + File.separator + "autoclassify.bat";
289: File f = new File(batchName);
290: if (!f.exists()) {
291: try {
292: PrintWriter out = new PrintWriter(
293: new FileOutputStream(f));
294: out.println("@echo off");
295: out.println("call " + psBaseDir
296: + File.separator + "lib"
297: + File.separator + "psEnv.bat");
298: out.println("set PATH = %SEARCH_PATH%");
299: out
300: .println("%JAVA_HOME%\\bin\\java -Xms32m -Xmx1000m -D\"java.protocol.handler.pkgs=com.iplanet.services.comm\" -cp \"%CLASSPATH%;%SEARCH_CLASSPATH%;"
301: + psBaseDir
302: + "\\lib\\searchadmin.jar\" -Djava.library.path=\"%PATH%\" com.sun.portal.search.autoclassify.Autoclassify %*");
303: out.close();
304: } catch (Exception e) {
305:
306: }
307: }
308: }
309: String cmd = serverRoot + File.separator
310: + Platform.getCommand("run-cs-cli")
311: + " autoclassify -s " + serverURL + " -d " + dbname
312: + " -l " + localeStr;
313: logger.log(Level.SEVERE, cmd);
314: try {
315: Runtime rt = Runtime.getRuntime();
316: Process p = rt.exec(cmd);
317: BufferedReader buf = new BufferedReader(
318: new InputStreamReader(p.getInputStream(),
319: "UTF-8"));
320: String outLine = null;
321: StringBuffer sb = new StringBuffer();
322: while ((outLine = buf.readLine()) != null) {
323: sb.append(outLine + "\n");
324: }
325: return sb.toString();
326: } catch (Exception e) {
327: return e.toString();
328: }
329:
330: }
331: }
332:
333: public String getConfigString(String config)
334: throws PSMBeanException, java.net.UnknownHostException {
335: if (!AdminUtil.isLocal(host)) {
336: try {
337: JMXConnector jmxc = AdminServerUtil
338: .getJMXConnector(host);
339: MBeanServerConnection msc = jmxc
340: .getMBeanServerConnection();
341: Object[] params = { config };
342: String[] signatures = { "java.lang.String" };
343: String result = (String) msc.invoke(objectName,
344: "getConfigString", params, signatures);
345: jmxc.close();
346: return result;
347: } catch (Exception e) {
348: logger.log(Level.SEVERE, "PSSH_CSPSAMB0003", e);
349: throw new PSMBeanException(
350: "Autoclassify:getConfigString", e.toString(), e);
351: }
352: } else {
353: try {
354:
355: //get the SearchServer associated with the Autoclassify Mbean
356: MBeanServer ms = PASModule.getMBeanServer();
357: LinkedList serverpath = new LinkedList();
358: serverpath.addFirst(path.get(2));
359: serverpath.addFirst(path.get(1));
360: ObjectName objName = AdminUtil
361: .getResourceMBeanObjectName(
362: AdminUtil.SEARCHSERVER_MBEAN_TYPE,
363: serverpath);
364:
365: String[] signature = { "java.lang.String" };
366:
367: if (config.equalsIgnoreCase("logPath")) {
368:
369: Object[] params1 = { AutoclassifyConfig.NAME_LOGFILE };
370:
371: String logPath = (String) ms.invoke(objName,
372: "getConfigValue", params1, signature);
373: if (logPath == null) {
374: String logDir = (String) ms.getAttribute(
375: objName, "LogDir");
376: logPath = logDir
377: + File.separator
378: + AutoclassifyConfig.DEFAULT_LOGFILE_NAME;
379: }
380:
381: return logPath;
382: }
383:
384: if (config.equalsIgnoreCase("dbPath")) {
385:
386: Object[] params2 = { AutoclassifyConfig.NAME_DBFILE };
387:
388: String dbPath = (String) ms.invoke(objName,
389: "getConfigValue", params2, signature);
390: if (dbPath == null) {
391: String tmpDir = (String) ms.getAttribute(
392: objName, "TmpDir");
393: dbPath = tmpDir
394: + File.separator
395: + AutoclassifyConfig.DEFAULT_DBFILE_NAME;
396: }
397:
398: return dbPath;
399: }
400:
401: if (config.equalsIgnoreCase("cacheSize")) {
402:
403: Object[] params3 = { AutoclassifyConfig.NAME_HASHTABLE_SIZE };
404:
405: String hashSize = (String) ms.invoke(objName,
406: "getConfigValue", params3, signature);
407: if (hashSize == null) {
408: hashSize = Integer
409: .toString(AutoclassifyConfig.DEFAULT_HASHTABLE_SIZE);
410: }
411:
412: return hashSize;
413: }
414:
415: } catch (Exception e) {
416: //logger.logp(Level.SEVERE, "Autoclassify", "getAutoclassConfig()", "failed to get autoclassify configuration " + config + "\n" + e.toString(), e);
417: logger.log(Level.SEVERE, "PSSH_CSPSAMB0003", e);
418: throw new PSMBeanException(
419: "errorcode.search.autoclassify.config",
420: "Autoclassify.getConfigString()", e);
421: }
422:
423: return null;
424: }
425: }
426:
427: private String[] split(String str, String deli) {
428: StringTokenizer st = new StringTokenizer(str, deli);
429: ArrayList list = new ArrayList();
430: int index = 0;
431: while (st.hasMoreTokens()) {
432: list.add(st.nextToken());
433: }
434: return (String[]) list.toArray(new String[0]);
435: }
436:
437: }
|