View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.jetspeed.deployment.impl;
18  
19  import java.io.File;
20  import java.io.IOException;
21  import java.io.InputStream;
22  import java.util.zip.ZipEntry;
23  import java.util.zip.ZipFile;
24  
25  import org.apache.jetspeed.deployment.DeploymentObject;
26  
27  /***
28   * <p>
29   * DeploymentObject
30   * </p>
31   * 
32   * @author <a href="mailto:weaver@apache.org">Scott T. Weaver </a>
33   * @version $Id: StandardDeploymentObject.java 516448 2007-03-09 16:25:47Z ate $
34   */
35  public class StandardDeploymentObject implements DeploymentObject
36  {
37      protected File    deploymentObject;
38      protected ZipFile zipFile;
39  
40      /***
41       * @throws IOException
42       */
43      public StandardDeploymentObject(File deploymentObject) throws FileNotDeployableException
44      {
45          if (verifyExtension(deploymentObject))
46          {
47              this.deploymentObject = deploymentObject;
48          }
49          else
50          {
51              throw new FileNotDeployableException("File type for " + deploymentObject.getName()
52                                                   + " is not supported by StandardDeploymentObject.");
53          }
54  
55      }
56  
57      /***
58       * <p>
59       * close
60       * </p>
61       * 
62       * @see org.apache.jetspeed.deployment.DeploymentObject#close()
63       * @throws IOException
64       */
65      public void close() throws IOException
66      {
67          if (zipFile != null)
68          {
69              zipFile.close();
70              zipFile = null;
71          }
72      }
73  
74      /***
75       * <p>
76       * getConfiguration
77       * </p>
78       * 
79       * @see org.apache.jetspeed.deployment.DeploymentObject#getConfiguration(java.lang.String)
80       * @param configPath
81       * @return
82       * @throws IOException
83       */
84      public InputStream getConfiguration(String configPath) throws IOException
85      {
86          ZipFile zipFile = getZipFile();
87          ZipEntry entry = zipFile.getEntry(configPath);
88          if (entry != null)
89          {
90              return zipFile.getInputStream(entry);
91          }
92          return null;
93      }
94  
95      /***
96       * <p>
97       * getName
98       * </p>
99       * 
100      * @see org.apache.jetspeed.deployment.DeploymentObject#getName()
101      * @return
102      */
103     public String getName()
104     {
105         return deploymentObject.getName();
106     }
107 
108     /***
109      * <p>
110      * getPath
111      * </p>
112      * 
113      * @see org.apache.jetspeed.deployment.DeploymentObject#getPath()
114      * @return
115      */
116     public String getPath()
117     {
118         return deploymentObject.getAbsolutePath();
119     }
120 
121     public ZipFile getZipFile() throws IOException
122     {
123         if (zipFile == null)
124         {
125             zipFile = new ZipFile(deploymentObject);
126         }
127         return zipFile;
128     }
129 
130     public File getFile()
131     {
132         return deploymentObject;
133     }
134 
135     protected boolean verifyExtension(File file)
136     {
137         String fileName = file.getName();
138         int dot = fileName.lastIndexOf('.');
139         if (dot != -1)
140         {
141             String ext = fileName.substring(dot);
142             return ext.equals(".war") || ext.equals(".jar") || ext.equals(".zip");
143         }
144         else
145         {
146             return false;
147         }
148     }
149 
150 }