001: /**
002: * $Id: Domain.java,v 1.30 2007/01/26 03:48:32 portalbld Exp $
003: * Copyright 2004 Sun Microsystems, Inc. All
004: * rights reserved. Use of this product is subject
005: * to license terms. Federal Acquisitions:
006: * Commercial Software -- Government Users
007: * Subject to Standard License Terms and
008: * Conditions.
009: *
010: * Sun, Sun Microsystems, the Sun logo, and Sun ONE
011: * are trademarks or registered trademarks of Sun Microsystems,
012: * Inc. in the United States and other countries.
013: */package com.sun.portal.fabric.tasks;
014:
015: import java.io.File;
016: import java.io.FileInputStream;
017: import java.io.FileWriter;
018: import java.io.FilenameFilter;
019: import java.io.IOException;
020: import java.util.Random;
021: import java.util.Set;
022: import java.util.HashSet;
023: import java.util.Iterator;
024: import java.util.Properties;
025: import java.util.logging.Logger;
026: import java.util.logging.Level;
027:
028: import com.sun.portal.log.common.PortalLogger;
029:
030: import com.sun.portal.fabric.util.FileUtil;
031: import com.sun.portal.fabric.util.ExecuteUtil;
032: import com.sun.portal.fabric.util.AMUtil;
033: import com.sun.portal.util.LDAPUtil;
034:
035: import com.sun.portal.admin.common.context.PSConfigContext;
036: import com.sun.portal.admin.common.PSConfigConstants;
037: import com.sun.portal.admin.common.Tags;
038:
039: import com.sun.portal.util.Platform;
040:
041: import com.iplanet.sso.SSOToken;
042:
043: import netscape.ldap.LDAPConnection;
044: import netscape.ldap.LDAPException;
045:
046: /**
047: * This class provides methods to configure
048: * the Cacao Node Agent and load the Portal
049: * Services into the Identity Server
050: */
051: public class Domain {
052:
053: private static final String fs = Platform.fs;
054: private static final Properties svcConf = new Properties();
055: private static final String DOMAIN_RB_NAME = "psDomainServices";
056: private PSConfigContext pcc;
057: private static Logger logger = PortalLogger.getLogger(Domain.class);
058: private ExecuteUtil execUtil;
059: private String psBaseDir;
060: private String exportLocation;
061: private String psLibDir;
062: private String sraServerDomain = null;
063:
064: /**
065: * Constructor for Domain
066: *
067: * @param psDataDir Portal Server data directory path as String
068: * @param psProductDir Portal Server installation directory path as String
069: * @param isProductDir Identity Server installation directory path as String
070: * @param cacaoProductDir Cacao framework install basedir path as String
071: */
072: public Domain(PSConfigContext pcc) {
073:
074: this .pcc = pcc;
075: execUtil = new ExecuteUtil();
076: psBaseDir = pcc.getPSBaseDir();
077: exportLocation = psBaseDir + fs + "export";
078: psLibDir = psBaseDir + fs + "lib";
079:
080: String svcConfLocation = psLibDir + fs
081: + PSConfigConstants.PS_SERVICE_CONFIG_FILE;
082: try {
083: svcConf.load(new FileInputStream(svcConfLocation));
084: } catch (IOException ioe) {
085: logger.log(Level.SEVERE, "PSFB_CSPFT0224", ioe);
086: }
087:
088: }
089:
090: /**
091: * This method configures the Identity Server with Portal Services.
092: * The Identity Server configuration tasks completed
093: * by this method will make it possible to configure
094: * Portals on this Identity Server.
095: *
096: * @param amAdminPassword Identity Server
097: * amadmin user password
098: */
099: private void processDomainServices(Set domainSvcs, SSOToken ssoToken) {
100:
101: LDAPConnection conn = LDAPUtil.getLDAPConnection(pcc
102: .getDirectoryServerHost(),
103: pcc.getDirectoryServerPort(), pcc
104: .getUserDataStoreManager(), pcc
105: .getUserDataStoreManagerCredentials(), pcc
106: .isDomainDataSecure());
107: try {
108: String psTmpDir = pcc.getPSDataDir() + fs + "tmp";
109: String svcTmpDirLoc = psTmpDir + fs
110: + FileUtil.getRandomDirName();
111:
112: // Create the tmp dir
113: File svcTmpDirDir = new File(svcTmpDirLoc);
114: logger.log(Level.FINEST, "PSFB_CSPFT0225", svcTmpDirLoc);
115: if (!svcTmpDirDir.mkdirs()) {
116: logger.log(Level.SEVERE, "PSFB_CSPFT0226");
117: }
118:
119: Iterator domainSvcsItr = domainSvcs.iterator();
120:
121: info("Parsing the file that associates Services to LDIFs");
122: String svcLDIFMapperLocation = psLibDir + fs
123: + PSConfigConstants.PS_SVC_LDIF_MAP_FILE;
124: Properties svcToLDIFFileMap = new Properties();
125: // Load the property file that maps service names to LDIF files
126: // that are associated with a service
127: svcToLDIFFileMap.load(new FileInputStream(
128: svcLDIFMapperLocation));
129:
130: info("Parsing the file that associate Services to SchemaXMLs");
131: String svcXMLMapperLocation = psLibDir + fs
132: + PSConfigConstants.PS_SVC_SCHEMA_MAP_FILE;
133: Properties svcToXmlFileMap = new Properties();
134: // Load the property file that maps service names to Schema XML
135: // file associated with that service
136: svcToXmlFileMap.load(new FileInputStream(
137: svcXMLMapperLocation));
138:
139: // Define a Set which will contain the paths of service schema
140: // xml files
141: Set svcXmlFilePaths = new HashSet();
142:
143: // For each service in the domainSvcs Set
144: // 1. Pickup the LDIF files associated with that service and
145: // load them into the Directory Server
146: // 2. Pickup the Service Schema XML file associated with that
147: // service and add it to the svcXmlFiles array.
148: while (domainSvcsItr.hasNext()) {
149:
150: String svcName = (String) domainSvcsItr.next();
151:
152: // From the svcToLDIFFileMap pickup the value associated
153: // with the current service name
154: String svcLDIFFiles = svcToLDIFFileMap.getProperty(
155: svcName, "");
156:
157: // A single service may be associated with multiple LDIF
158: // files. In that case the value is a space separated list
159: // of LDIF files. Always tokenize the value that there in
160: // the map that is defined in the property file.
161: svcLDIFFiles.trim();
162: String[] ldifFileNames = null;
163: if (svcLDIFFiles.length() > 0) {
164: ldifFileNames = svcLDIFFiles.split("[\\s]+");
165: }
166: int iSize0 = (ldifFileNames != null) ? ldifFileNames.length
167: : 0;
168: // Load each of these LDIF files. Usually a service is only
169: // associated with one LDIF file except for WSRP service
170: for (int i = 0; i < iSize0; i++) {
171: // Create a full path for the LDIF file to be loaded
172: String svcLDIFPath = exportLocation + fs + "ldif"
173: + fs + ldifFileNames[i];
174:
175: File svcLDIF = new File(svcLDIFPath);
176: if (!svcLDIF.exists()) {
177: info("LDIF FileNotFound for Service: "
178: + svcName);
179: continue;
180: }
181: // Load the Portal Domain Service Related Objectclasses
182: // into the Directory Server using the LDAPUtils
183: info("Loading Objectclasses defined in:"
184: + svcLDIFPath);
185: LDAPUtil.loadLDIF(svcLDIFPath, conn);
186: }
187:
188: // From the svcToXmlFileMap pickup the value associated
189: // with the current service name
190: String svcSchemaXMLFile = svcToXmlFileMap.getProperty(
191: svcName, "");
192: // Check if the schema XML file mapping exists for service
193: if (!svcSchemaXMLFile.equals("")) {
194: // Create a full path of the service XML file
195: String svcXMLFilePath = exportLocation + fs
196: + "service" + fs + svcSchemaXMLFile;
197: File svcXML = new File(svcXMLFilePath);
198: // Check if the file really exists on the filesystem
199: // add it to the set of service of XML files
200: if (svcXML.exists()) {
201: if (sraServerDomain != null
202: && svcSchemaXMLFile.indexOf("srap") != -1
203: && svcTmpDirDir.exists()) {
204:
205: String destSvcTmpFilePath = svcTmpDirLoc
206: + fs + svcSchemaXMLFile;
207: File destSvcTmpFile = new File(
208: destSvcTmpFilePath);
209:
210: // Copying the srap service files to tmp dir
211: logger.log(Level.FINEST, "PSFB_CSPFT0228",
212: new Object[] { svcXMLFilePath,
213: destSvcTmpFilePath });
214: FileUtil.copyFile(svcXML, destSvcTmpFile);
215:
216: logger.log(Level.FINEST, "PSFB_CSPFT0229",
217: destSvcTmpFilePath);
218: FileUtil.replaceTokenInFile(destSvcTmpFile,
219: Tags.SRA_SERVER_DOMAIN,
220: sraServerDomain);
221:
222: svcXmlFilePaths.add(destSvcTmpFilePath);
223:
224: } else {
225:
226: svcXmlFilePaths.add(svcXMLFilePath);
227:
228: }
229: } else {
230: logger.log(Level.INFO, "PSFB_CSPFT0230",
231: svcName);
232: }
233: } else {
234: logger.log(Level.SEVERE, "PSFB_CSPFT0231", svcName);
235: }
236:
237: }
238:
239: info("Loading Portal Services into Access Manager...");
240: // Load the service schema XMLs using the AMTaskUtil
241: AMUtil.loadServiceSchema(svcXmlFilePaths, ssoToken);
242:
243: //Delete the tmp dir
244: FileUtil.deleteDir(svcTmpDirDir);
245:
246: // Load ResouceBundles for Domain Services
247: info("Loading I18N for Domain Services");
248: File rbLoc = new File(exportLocation + fs + "locale");
249: File[] localeFileList = rbLoc.listFiles();
250: for (int i = 0; i < localeFileList.length; i++) {
251: try {
252: // Get the full path and the file name
253: String fname = localeFileList[i].getName();
254: String fPath = localeFileList[i].getAbsolutePath();
255: String fileLocale = null;
256: // If file is a properties file and its name contains
257: // the word psDomainServices then load it
258: if (fname.startsWith(DOMAIN_RB_NAME)
259: && fname.endsWith(".properties")) {
260:
261: // Prepare a properties object from locale file
262: Properties props = new Properties();
263: FileInputStream fis = new FileInputStream(fPath);
264: props.load(fis);
265:
266: // Get the locale from the file name
267: int beginFileLocale = fname.indexOf('_') + 1;
268: int endFileLocale = fname
269: .indexOf(".properties");
270: if (beginFileLocale > 0) {
271: fileLocale = fname.substring(
272: beginFileLocale, endFileLocale);
273: }
274: AMUtil.addResourceBundle(DOMAIN_RB_NAME,
275: fileLocale, props, ssoToken);
276: }
277: } catch (Exception e) {
278: logger.log(Level.SEVERE, "PSFB_CSPFT0232", e);
279: }
280: }
281:
282: } catch (Exception e) {
283: logger.log(Level.SEVERE, "PSFB_CSPFT0233", e);
284: }
285: }
286:
287: public void configureMandatoryServices(SSOToken ssoToken) {
288:
289: // Get the list of portal domain services
290: Set domainSvcs = getMandatoryDomainServices();
291:
292: // Load LDIFs, tagswap and load service schema for mandatory domain
293: // services
294: processDomainServices(domainSvcs, ssoToken);
295:
296: }
297:
298: public void configureSRAServices(SSOToken ssoToken, String sraDomain) {
299:
300: sraServerDomain = sraDomain;
301: // Get the list of sra domain services
302: Set sraDomainSvcs = getSRADomainServices();
303:
304: // Load LDIFs, tagswap and load service schema for sra domain
305: // services
306: processDomainServices(sraDomainSvcs, ssoToken);
307: }
308:
309: /**
310: * This method removes the Portal Domain services from the Access Manager
311: * @param amAdminPassword Identity Server
312: * amadmin user password
313: */
314: public void removeDomainServices(SSOToken ssoToken) {
315:
316: // Get the list of portal domain services
317: Set domainSvcs = getMandatoryDomainServices();
318: // Make the delete call on the util to remove the services
319: AMUtil.deleteServiceSchema(domainSvcs, ssoToken);
320: // Remove the resource bundles loaded for this portal into AM
321: AMUtil.removeResourceBundle(DOMAIN_RB_NAME, null, ssoToken);
322:
323: }
324:
325: /**
326: * This method removes the SRA services from the Access Manager
327: * @param amAdminPassword Identity Server
328: * amadmin user password
329: */
330: public void removeSRAServices(SSOToken ssoToken) {
331:
332: // Get the list of portal domain services
333: Set domainSvcs = getSRADomainServices();
334: // Make the delete call on the util to remove the services
335: AMUtil.deleteServiceSchema(domainSvcs, ssoToken);
336: }
337:
338: /**
339: * This method fetches the list of mandatory domain services from the
340: * Service configuration property file and returns the
341: * services names in a String array
342: */
343: private Set getMandatoryDomainServices() {
344:
345: // Load the property file that defines the set Domain Services
346: info("Fetching the list of Portal Domain Services");
347: // Get the value of the properties that defines the list of
348: // mandatory Portal Domain Services
349: String domainSvcsStr = svcConf
350: .getProperty("DomainServices", "");
351: domainSvcsStr.trim();
352: // The value of the domain services string buffer is a space
353: // separated string of service names. Create a tokenizer to
354: // tokenize the string into individual service names
355: String[] domainSvc = null;
356: if (domainSvcsStr.length() > 0) {
357: domainSvc = domainSvcsStr.split("[\\s]+");
358: }
359: int iSize0 = (domainSvc != null) ? domainSvc.length : 0;
360: // Create a set that contains the domain service names
361: Set domainSvcs = new HashSet();
362: for (int i = 0; i < iSize0; i++) {
363: domainSvcs.add(domainSvc[i]);
364: }
365:
366: return domainSvcs;
367: }
368:
369: /**
370: * This method fetches the list of optional domain services from the
371: * Service configuration property file and returns the
372: * services names in a String array
373: */
374: private Set getSRADomainServices() {
375:
376: // Load the property file that defines the set Domain Services
377: info("Fetching the list of SRA Domain Services");
378: // Get the value of the properties that defines the list of
379: // mandatory Portal Domain Services
380: String sraSvcsStr = svcConf
381: .getProperty("SRADomainServices", "");
382: sraSvcsStr.trim();
383: // The value of the sra domain services string buffer is a space
384: // separated string of service names. Create a tokenizer to
385: // tokenize the string into individual service names
386: String[] sraSvc = null;
387: if (sraSvcsStr.length() > 0) {
388: sraSvc = sraSvcsStr.split("[\\s]+");
389: }
390: int iSize0 = (sraSvc != null) ? sraSvc.length : 0;
391:
392: // Create a set that contains the sra domain service names
393: Set sraDomainSvcs = new HashSet();
394: for (int i = 0; i < iSize0; i++) {
395: sraDomainSvcs.add(sraSvc[i]);
396: }
397:
398: return sraDomainSvcs;
399: }
400:
401: private final void info(String message) {
402: if (logger.isLoggable(Level.INFO)) {
403: logger.info(message);
404: }
405: }
406: }
|