001: package com.sun.portal.fabric.tasks;
002:
003: import java.io.*;
004: import java.util.*;
005: import java.util.zip.*;
006: import java.util.jar.*;
007: import java.util.regex.*;
008: import java.lang.*;
009:
010: import java.util.logging.Level;
011: import java.util.logging.Logger;
012:
013: import com.sun.portal.admin.server.AdminServerUtil;
014: import com.sun.portal.admin.common.context.PSConfigContext;
015: import com.sun.portal.admin.common.PSConfigConstants;
016: import com.sun.portal.admin.common.InstanceAttributes;
017: import com.sun.portal.fabric.util.FileUtil;
018: import com.sun.portal.fabric.util.ExecuteUtil;
019: import com.sun.portal.fabric.util.AMTaskUtil;
020: import com.sun.portal.fabric.util.LogConfigUtil;
021: import com.sun.portal.fabric.tasks.WebContainer;
022: import com.sun.portal.fabric.tasks.WebContainerFactory;
023: import com.sun.portal.util.Platform;
024:
025: import com.sun.portal.log.common.PortalLogger;
026:
027: public class SearchSetup {
028:
029: private static final String searchServerDomainDirName = "searchservers";
030: private static final String templateWarfileName = "rdm.war";
031: private static final String fs = Platform.fs;
032:
033: private String javaHomeDirName = null;
034: private String jssnssDirName = null;
035: private String ktSearchDirName = null;
036: private String isBaseDirName = null;
037: private String psDirName = null;
038: private String psConfDirName = null;
039: private String psDataDirName = null;
040: private String searchServerID = null;
041: private String uri = null;
042: private Properties webContainerProperties = null;
043: private String amAdminUserDN = null;
044: private String amAdminPassword = null;
045: private PSConfigContext configContext = null;
046: private boolean isWindows = false;
047: private boolean isHpux = false;
048: private String arch_64bit = "32";
049: private static Logger logger = PortalLogger
050: .getLogger(SearchSetup.class);
051:
052: public SearchSetup() {
053: this .isWindows = isWindowsOS();
054: this .isHpux = isHpuxOS();
055: }
056:
057: public void configuration(final PSConfigContext configContext,
058: final String searchServerID,
059: final Properties webContainerProperties) {
060: this .configContext = configContext;
061: this .javaHomeDirName = configContext.getJavaHome();
062: this .jssnssDirName = configContext.getJSSNSSLibDir();
063: this .ktSearchDirName = configContext.getPrivateSharedLibsDir();
064: this .psDirName = configContext.getPSBaseDir();
065: this .psDataDirName = configContext.getPSDataDir();
066: this .searchServerID = searchServerID;
067: this .webContainerProperties = webContainerProperties;
068:
069: Platform.psConfDir = configContext.getPSConfigDir();
070:
071: try {
072: // check if the WebContainer is a 64 bits iWS7
073: Map attributes = new HashMap(webContainerProperties);
074: WebContainer wc = WebContainerFactory.getWebContainer(
075: attributes, true);
076: if (wc.getWebContainerType().equals(
077: WebContainer.TYPE_JESWS7)) {
078: arch_64bit = ((JESWS7Impl) wc).getPlatform();
079: } else {
080: arch_64bit = "32";
081: }
082: configureInstance();
083: } catch (Exception e) {
084: logger.log(Level.SEVERE, "PSFB_CSPFT0304", e);
085: }
086: }
087:
088: public void deployment(final PSConfigContext configContext,
089: final String searchServerID, final String uri,
090: final Properties webContainerProperties) {
091: this .configContext = configContext;
092: this .isBaseDirName = configContext.getISBaseDir();
093: this .psDirName = configContext.getPSBaseDir();
094: this .psConfDirName = configContext.getPSConfigDir();
095: this .psDataDirName = configContext.getPSDataDir();
096: this .searchServerID = searchServerID;
097: this .uri = uri;
098: this .webContainerProperties = webContainerProperties;
099: this .amAdminUserDN = configContext.getAdminUserDN();
100: this .amAdminPassword = AdminServerUtil.getPassword();
101:
102: Platform.psConfDir = configContext.getPSConfigDir();
103:
104: try {
105: configureWebContainer();
106: copyWar();
107: configureWar();
108: deployWar();
109: } catch (ConfigurationException ce) {
110: logger.log(Level.SEVERE, null, ce);
111: } catch (IllegalArgumentException iae) {
112: logger.log(Level.SEVERE, null, iae);
113: } catch (Exception e) {
114: logger.log(Level.SEVERE, "PSFB_CSPFT0305", e);
115: }
116: }
117:
118: public void redeployment(final PSConfigContext configContext,
119: final String searchServerID, final String uri,
120: final Properties webContainerProperties) {
121: this .configContext = configContext;
122: this .isBaseDirName = configContext.getISBaseDir();
123: this .psDirName = configContext.getPSBaseDir();
124: this .psConfDirName = configContext.getPSConfigDir();
125: this .psDataDirName = configContext.getPSDataDir();
126: this .searchServerID = searchServerID;
127: this .uri = uri;
128: this .webContainerProperties = webContainerProperties;
129: this .amAdminUserDN = configContext.getAdminUserDN();
130: this .amAdminPassword = AdminServerUtil.getPassword();
131:
132: Platform.psConfDir = configContext.getPSConfigDir();
133:
134: try {
135: undeployWar();
136: copyWar();
137: configureWar();
138: deployWar();
139: } catch (ConfigurationException ce) {
140: logger.log(Level.SEVERE, null, ce);
141: } catch (IllegalArgumentException iae) {
142: logger.log(Level.SEVERE, null, iae);
143: } catch (Exception e) {
144: logger.log(Level.SEVERE,
145: "Failed to redeploy search server", e);
146: }
147: }
148:
149: public void delete(final PSConfigContext configContext,
150: final String searchServerID, final String uri,
151: final Properties webContainerProperties) {
152: this .configContext = configContext;
153: this .psDirName = configContext.getPSBaseDir();
154: this .psDataDirName = configContext.getPSDataDir();
155: this .searchServerID = searchServerID;
156: this .uri = uri;
157: this .webContainerProperties = webContainerProperties;
158:
159: Platform.psConfDir = configContext.getPSConfigDir();
160:
161: try {
162: undeployWar();
163: } catch (ConfigurationException ce) {
164: logger.log(Level.SEVERE, null, ce);
165: } catch (IllegalArgumentException iae) {
166: logger.log(Level.SEVERE, null, iae);
167: } catch (Exception e) {
168: logger.log(Level.SEVERE, "PSFB_CSPFT0306", e);
169: }
170:
171: String dirName = psDataDirName + fs + searchServerDomainDirName
172: + fs + searchServerID;
173: FileUtil.deleteDir(dirName);
174: }
175:
176: private void configureInstance() throws Exception {
177: String dirName = psDataDirName + fs + searchServerDomainDirName
178: + fs + searchServerID;
179:
180: FileUtil.makeDir(dirName + fs + "config");
181: FileUtil.makeDir(dirName + fs + "logs");
182: FileUtil.makeDir(dirName + fs + "robot");
183: FileUtil.makeDir(dirName + fs + "tmp");
184: FileUtil.makeDir(dirName + fs + "db");
185:
186: createServerRdm(dirName);
187:
188: if (isWindows) {
189: createScript(dirName, Platform.getCommand("run-cs-cli"),
190: "%*");
191: createJavaScript(dirName, "StartRobot.bat",
192: "com.sun.portal.search.admin.cli.RobotController \""
193: + dirName + "\" start");
194: createJavaScript(dirName, "StopRobot.bat",
195: "com.sun.portal.search.admin.cli.RobotController \""
196: + dirName + "\" stop");
197: } else {
198: createScript(dirName, Platform.getCommand("run-cs-cli"),
199: "\\$@");
200: createJavaScript(dirName, "StartRobot",
201: "com.sun.portal.search.admin.cli.RobotController \""
202: + dirName + "\" start");
203: createJavaScript(dirName, "StopRobot",
204: "com.sun.portal.search.admin.cli.RobotController \""
205: + dirName + "\" stop");
206: }
207:
208: createConfigurationFile(dirName, "search.conf");
209: createConfigurationFile(dirName, "robot.conf");
210: createConfigurationFile(dirName, "filter.conf");
211: createConfigurationFile(dirName, "import.conf");
212: createConfigurationFile(dirName, "filterrules.conf");
213: createConfigurationFile(dirName, "classification.conf");
214: createConfigurationFile(dirName, "convert.conf");
215: createConfigurationFile(dirName, "mime.types");
216: createConfigurationFile(dirName, "schema.rdm");
217: createConfigurationFile(dirName, "taxonomy.rdm");
218: createConfigurationFile(dirName, "converter.cfg");
219: createConfigurationFile(dirName, "converter.template");
220:
221: // Tagswap the pslogconfig properties and place it in the
222: // configuration directory of the search server
223: LogConfigUtil.createSearchLogConfigFile(psDirName,
224: psDataDirName, searchServerID);
225: logger.log(Level.FINEST, "PSFB_CSPFT0307",
226: PSConfigConstants.PS_LOG_CONFIG_FILE);
227:
228: String emptyFileName = dirName + fs + "tmp" + fs + "empty";
229:
230: FileOutputStream emptyFile = new FileOutputStream(emptyFileName);
231: emptyFile.close();
232:
233: String action[] = {
234: dirName + fs + Platform.getCommand("run-cs-cli"),
235: Platform.getCommand("rdmgr"), "-q", emptyFileName };
236:
237: Runtime.getRuntime().exec(action);
238: action[2] = "-T";
239: action[3] = dirName + fs + "config" + fs + "taxonomy.rdm";
240: Runtime.getRuntime().exec(action);
241: }
242:
243: private void createServerRdm(String dirName) throws Exception {
244: String action[] = { psDirName + fs + "lib" + fs + "buildsd" };
245: String envs[] = { "LD_LIBRARY_PATH=" + psDirName + fs + "lib" };
246: Process proc = Runtime.getRuntime().exec(action, envs);
247: BufferedReader br = new BufferedReader(new InputStreamReader(
248: proc.getInputStream()));
249: PrintWriter pw = new PrintWriter(new BufferedWriter(
250: new FileWriter(dirName + fs + "config" + fs
251: + "server.rdm")));
252: String line;
253: while ((line = br.readLine()) != null) {
254: pw.println(line);
255: }
256: pw.close();
257: br.close();
258: }
259:
260: private void createScript(String dirName, String script,
261: String command) throws Exception {
262:
263: String binDirName = psDirName + fs + "bin";
264: String libDirName = psDirName + fs + "lib";
265: String srcFileName = psDirName + fs + "samples" + fs + "config"
266: + fs + "Script.template";
267: String destFileName = dirName + fs
268: + Platform.getCommand(script);
269:
270: FileUtil.copyFile(srcFileName, destFileName);
271:
272: String[] ptrn = { "@dirName@", "@dirNameFs@", "@binDirName@",
273: "@libDirName@", "@command@" };
274:
275: String[] val = { dirName, dirName.replaceAll("/", "\\\\\\\\"),
276: binDirName, libDirName, command };
277:
278: FileUtil.replaceTokensInFile(new File(destFileName), ptrn, val);
279: FileUtil.changeFilePermissions(destFileName, "700");
280: }
281:
282: private void createJavaScript(String dirName, String script,
283: String command) throws Exception {
284:
285: String binDirName = psDirName + fs + "bin";
286: String libDirName = psDirName + fs + "lib";
287: String srcFileName = psDirName + fs + "samples" + fs + "config"
288: + fs + "JavaScript.template";
289: String destFileName = dirName + fs
290: + Platform.getCommand(script);
291:
292: FileUtil.copyFile(srcFileName, destFileName);
293:
294: String[] ptrn = { "@dirName@", "@javaHomeDirName@",
295: "@binDirName@", "@libDirName@", "@command@" };
296:
297: String[] val = { dirName, javaHomeDirName, binDirName,
298: libDirName, command };
299:
300: FileUtil.replaceTokensInFile(new File(destFileName), ptrn, val);
301: FileUtil.changeFilePermissions(destFileName, "700");
302: }
303:
304: private void createConfigurationFile(String dirName, String fileName)
305: throws Exception {
306: String binDirName = psDirName + fs + "bin";
307: String libDirName = psDirName + fs + "lib";
308: String srcDirName = psDirName + fs + "samples" + fs + "config";
309: String destDirName = dirName + fs + "config";
310:
311: BufferedReader br = new BufferedReader(new FileReader(
312: srcDirName + fs + fileName));
313: PrintWriter pw = new PrintWriter(new BufferedWriter(
314: new FileWriter(destDirName + fs + fileName)));
315: String line;
316: while ((line = br.readLine()) != null) {
317: if (isWindows || isHpux) {
318: line = FileUtil
319: .replaceToken(
320: line,
321: ":/usr/lib/mps/secv1:/usr/lib/mps:/usr/lib/lwp:/opt/sun/private/lib",
322: File.pathSeparator + "\\$CSJSSNSSLIB");
323: }
324: line = FileUtil.replaceToken(line, "\\$CSROOT", dirName);
325: line = FileUtil.replaceToken(line, "\\$CSBIN", binDirName);
326: line = FileUtil.replaceToken(line, "\\$CSLIB", libDirName);
327: line = FileUtil.replaceToken(line, "\\$CSJSSNSSLIB",
328: jssnssDirName);
329: line = FileUtil.replaceToken(line, "\\$CS64ARCH", ""
330: + arch_64bit);
331: pw.println(line);
332: }
333: br.close();
334: pw.close();
335: }
336:
337: private void configureWebContainer() throws Exception {
338: Map attributes = new HashMap(webContainerProperties);
339: WebContainer wc = WebContainerFactory.getWebContainer(
340: attributes, true);
341: wc.configureSearch(psDirName + fs + "lib" + fs + "db.jar",
342: configContext);
343: }
344:
345: private void copyWar() throws Exception {
346: String srcDirName = psDirName + fs + "export";
347: String srcFileName = srcDirName + fs + templateWarfileName;
348:
349: String destDirName = psDataDirName + fs
350: + searchServerDomainDirName + fs + searchServerID + fs
351: + "war";
352: String destFileName = destDirName + fs + uri + ".war";
353:
354: File destFile = new File(destFileName);
355: if (destFile.exists()) {
356: destFile.delete();
357: }
358:
359: FileUtil.makeDir(destDirName);
360: FileUtil.copyFile(srcFileName, destFileName);
361: }
362:
363: private void configureWar() throws Exception {
364: String warDirName = psDataDirName + fs
365: + searchServerDomainDirName + fs + searchServerID + fs
366: + "war";
367:
368: File originalFile = new File(warDirName + fs + uri + ".war");
369: JarFile originalJarFile = new JarFile(originalFile);
370:
371: File newFile = new File(warDirName + fs + uri + ".war.new");
372: JarOutputStream newJarFile = new JarOutputStream(
373: new FileOutputStream(newFile));
374:
375: Enumeration entries = originalJarFile.entries();
376: while (entries.hasMoreElements()) {
377: JarEntry entry = (JarEntry) entries.nextElement();
378: InputStream is = originalJarFile.getInputStream(entry);
379: if (entry.getName().equals("WEB-INF/web.xml")) {
380: JarEntry newEntry = new JarEntry("WEB-INF/web.xml");
381: newJarFile.putNextEntry(newEntry);
382: BufferedReader br = new BufferedReader(
383: new InputStreamReader(is));
384: StringBuffer sb = new StringBuffer();
385: String line;
386: while ((line = br.readLine()) != null) {
387: line = FileUtil.replaceToken(line,
388: "%PS_CONFIG_DIR%", psConfDirName);
389: line = FileUtil.replaceToken(line, "%PS_VAR_DIR%",
390: psDataDirName);
391: line = FileUtil.replaceToken(line, "%DOMAIN_DIR%",
392: searchServerDomainDirName);
393: line = FileUtil.replaceToken(line,
394: "%SEARCHSERVER_DIR%", searchServerID);
395: sb.append(line + "\n");
396: }
397: newJarFile.write(sb.toString().getBytes(), 0, sb
398: .length());
399: } else {
400: newJarFile.putNextEntry(entry);
401: int bytesRead;
402: byte[] buffer = new byte[1024];
403: while ((bytesRead = is.read(buffer)) != -1) {
404: newJarFile.write(buffer, 0, bytesRead);
405: }
406: }
407: }
408:
409: try {
410: String srcFileName = configContext
411: .getPrivateSharedLibsDir()
412: + fs + "ktsearch.jar";
413: String destFileName = "WEB-INF" + fs + "lib" + fs
414: + "ktsearch.jar";
415:
416: JarEntry je = new JarEntry(destFileName);
417: newJarFile.putNextEntry(je);
418:
419: byte buffer[] = new byte[1024];
420: BufferedInputStream bis = new BufferedInputStream(
421: new FileInputStream(srcFileName), buffer.length);
422:
423: int bytesRead;
424: while ((bytesRead = bis.read(buffer, 0, buffer.length)) != -1) {
425: newJarFile.write(buffer, 0, bytesRead);
426: }
427: } catch (Exception e) {
428: }
429:
430: try {
431: String srcFileName = psDirName + fs + "lib" + fs
432: + "pslogcommon.jar";
433: String destFileName = "WEB-INF" + fs + "lib" + fs
434: + "pslogcommon.jar";
435:
436: JarEntry je = new JarEntry(destFileName);
437: newJarFile.putNextEntry(je);
438:
439: byte buffer[] = new byte[1024];
440: BufferedInputStream bis = new BufferedInputStream(
441: new FileInputStream(srcFileName), buffer.length);
442:
443: int bytesRead;
444: while ((bytesRead = bis.read(buffer, 0, buffer.length)) != -1) {
445: newJarFile.write(buffer, 0, bytesRead);
446: }
447: } catch (Exception e) {
448: }
449:
450: originalJarFile.close();
451: newJarFile.close();
452:
453: originalFile.delete();
454: newFile.renameTo(originalFile);
455: }
456:
457: private void deployWar() throws Exception {
458: String warDirName = psDataDirName + fs
459: + searchServerDomainDirName + fs + searchServerID + fs
460: + "war";
461: Map attributes = new HashMap(webContainerProperties);
462: WebContainer wc = WebContainerFactory.getWebContainer(
463: attributes, true);
464: wc.deploy(warDirName + fs + uri + ".war", "/" + uri);
465: }
466:
467: private void undeployWar() throws Exception {
468: Map attributes = new HashMap(webContainerProperties);
469: WebContainer wc = WebContainerFactory.getWebContainer(
470: attributes, true);
471: wc.undeploy("/" + uri);
472: }
473:
474: private boolean isWindowsOS() {
475: String osName = Platform.name;
476: return osName.startsWith("windows");
477: }
478:
479: private boolean isHpuxOS() {
480: String osName = Platform.name;
481: return osName.startsWith("HP-UX");
482: }
483:
484: }
|