KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > deployment > deploy > shared > ArchiveFactory


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.enterprise.deployment.deploy.shared;
25
26 import java.io.IOException JavaDoc;
27 import java.net.URI JavaDoc;
28 import java.net.MalformedURLException JavaDoc;
29 import java.io.File JavaDoc;
30
31 /**
32  * This implementation of the AbstractArchiveFactory interface
33  * is capable of creating the right abstraction of the Archive
34  * interface depending on the protocol used in the URL.
35  *
36  * @author Jerome Dochez
37  */

38 public class ArchiveFactory implements AbstractArchiveFactory {
39     
40     /** Creates a new instance of ArchiveFactory */
41     public ArchiveFactory() {
42     }
43     
44             
45     public AbstractArchive createArchive(String JavaDoc path) throws java.io.IOException JavaDoc {
46         try {
47             /*
48              *Use the expanded constructor so illegal characters (such as embedded blanks) in the path
49              *will be encoded.
50              */

51             return createArchive(prepareArchiveURI(path));
52         } catch(java.net.URISyntaxException JavaDoc e) {
53             return null;
54         }
55     }
56     
57     public AbstractArchive openArchive(String JavaDoc path) throws java.io.IOException JavaDoc {
58         try {
59             return openArchive(prepareArchiveURI(path));
60         } catch(java.net.URISyntaxException JavaDoc e) {
61             return null;
62         }
63     }
64     
65     /**
66      * Creates a new archivist using the URL as the path. The URL
67      * protocol will define the type of desired archive (jar, file, etc)
68      * @param url to the archive
69      * @return the apropriate archive
70      */

71     public AbstractArchive createArchive(URI JavaDoc path) throws IOException JavaDoc {
72         
73         String JavaDoc protocol = path.getScheme();
74         if (protocol.equals("file")) {
75             FileArchive output = new FileArchive();
76             output.create(path.getPath());
77             return output;
78         } else
79         if (protocol.equals("jar")) {
80             OutputJarArchive ja = new OutputJarArchive();
81             ja.create(path.getPath());
82             return ja;
83         } else
84         throw new MalformedURLException JavaDoc("Protocol not supported : " + protocol);
85     }
86     
87     /**
88      * Opens an existing archivist using the URL as the path.
89      * The URL protocol will defines the type of desired archive
90      * (jar, file, memory, etc...)
91      * @param url to the existing archive
92      * @return the appropriate archive
93      */

94     public AbstractArchive openArchive(URI JavaDoc path) throws IOException JavaDoc {
95         
96         String JavaDoc protocol = path.getScheme();
97         if (protocol.equals("file")) {
98             FileArchive input = new FileArchive();
99             input.open(path.getPath());
100             return input;
101         } else
102         if (protocol.equals("jar")) {
103             InputJarArchive ja = new InputJarArchive();
104             ja.open(path.getPath());
105             return ja;
106         } else
107 /* if (protocol.equals("mem")) {
108             MemoryMappedArchive mapped = new MemoryMappedArchive();
109             mapped.open(path.getPath());
110             return mapped;
111         }*/

112         throw new MalformedURLException JavaDoc("Protocol not supported : " + protocol);
113     }
114     
115     /**
116      *Create a URI for the jar specified by the path string.
117      *<p>
118      *The steps used here correctly encode "illegal" characters - such as embedded blanks - in
119      *the path string that otherwise would render the URI unusable. The URI constructor that
120      *accepts just the path string does not perform this encoding.
121      *@param path string for the archive
122      *@return URI with any necessary encoding of special characters
123      */

124     static java.net.URI JavaDoc prepareArchiveURI(String JavaDoc path) throws java.net.URISyntaxException JavaDoc, java.io.UnsupportedEncodingException JavaDoc, java.io.IOException JavaDoc {
125        
126         File JavaDoc archiveFile = new File JavaDoc(path);
127         URI JavaDoc archiveURI = archiveFile.toURI();
128         String JavaDoc scheme = (archiveFile.isDirectory() ? "file" : "jar");
129         URI JavaDoc answer = new URI JavaDoc(scheme, null /* authority */, archiveURI.getPath(), null /* query */, null /* fragment */);
130         return answer;
131     }
132 }
133
Popular Tags