001: /**
002: * $Id: DeploymentDescriptorFinder.java,v 1.2 2006/02/10 09:00:56 nk137934 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.portlet.admin.mbeans.tasks;
014:
015: import java.io.File;
016: import java.io.FileFilter;
017: import java.io.FileOutputStream;
018: import java.io.InputStream;
019: import java.io.IOException;
020: import java.io.FileNotFoundException;
021: import java.util.List;
022: import java.util.ArrayList;
023: import java.util.Enumeration;
024: import java.util.Properties;
025: import java.util.StringTokenizer;
026: import java.util.jar.Manifest;
027: import java.util.jar.JarEntry;
028: import java.util.jar.JarFile;
029: import java.util.jar.JarOutputStream;
030: import java.util.logging.Logger;
031: import java.util.logging.Level;
032:
033: // This utility class is used to find all the portlet xml files for a
034: // given ear application
035: public class DeploymentDescriptorFinder {
036:
037: private static final String DD_LOCATION = "DDFileLocation";
038:
039: private Properties configProps = null;
040: private JarFile earFile = null;
041: private String earApplicationName = null;
042: private String ddLocation = null;
043: private boolean verbose = false;
044: private Logger logger = null;
045:
046: public DeploymentDescriptorFinder(String earApplicationName,
047: Properties props, boolean verbose, Logger logger) {
048:
049: this .earApplicationName = earApplicationName;
050: this .configProps = props;
051: //location of portlet tmp dir
052: ddLocation = configProps.getProperty(DD_LOCATION);
053: this .verbose = verbose;
054: this .logger = logger;
055: }
056:
057: // List of type File - represents all portlet xml in the ear app
058: // If any exception occurs, an empty list is returned
059: public List findPortletDescriptors()
060: throws PortletDeployerException {
061:
062: if (verbose) {
063: logger.log(Level.INFO, "PSPL_CSPPAMT0019",
064: new Object[] { earApplicationName });
065: }
066: List descriptors = new ArrayList();
067: File parentDir = new File(ddLocation);
068: File[] files = parentDir.listFiles(new DDFileFilter());
069: if (files.length == 0)
070: throw new PortletDeployerException("noRegisteredPortlets");
071: for (int i = 0; i < files.length; i++) {
072: descriptors.add(files[i]);
073: }
074: if (verbose) {
075: logger.log(Level.INFO, "PSPL_CSPPAMT0020", new Integer(
076: descriptors.size()));
077: }
078: return descriptors;
079: }
080:
081: class DDFileFilter implements FileFilter {
082: public boolean accept(File file) {
083:
084: // Tokenize the file name with "_"
085: StringTokenizer tokenizer = new StringTokenizer(file
086: .getName(), "_");
087: if (tokenizer.countTokens() == 3) {
088: if (getEarApplicationName(file.getName()).equals(
089: earApplicationName)) {
090: return true;
091: } else {
092: return false;
093: }
094: } else {
095: return false;
096: }
097: }
098: }
099:
100: // Given the stored portlet xml name, returns the ear application name.
101: // The input should be a valid name (i.e 3 "_")
102: private String getEarApplicationName(String portletXMLName) {
103:
104: /*String thisEarName = null;
105: StringTokenizer tokenizer = new StringTokenizer(portletXMLName,"_");
106: thisEarName = tokenizer.nextToken();
107: */
108: StringBuffer this EarName = new StringBuffer();
109: StringTokenizer tokenizer = new StringTokenizer(portletXMLName,
110: "_");
111: int count = tokenizer.countTokens();
112: for (int k = 0; k < count - 2; k++) {
113: this EarName.append(tokenizer.nextToken());
114: if (k != count - 3) {
115: this EarName.append("_");
116: }
117: }
118: return thisEarName.toString();
119: }
120: }
|