KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > system > repository > ReadOnlyRepository


1 /**
2  *
3  * Copyright 2003-2004 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * 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
18 package org.apache.geronimo.system.repository;
19
20 import java.io.File JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.net.MalformedURLException JavaDoc;
23 import java.net.URI JavaDoc;
24 import java.net.URL JavaDoc;
25
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28 import org.apache.geronimo.gbean.GBeanInfo;
29 import org.apache.geronimo.gbean.GBeanInfoBuilder;
30 import org.apache.geronimo.gbean.GBeanLifecycle;
31 import org.apache.geronimo.kernel.repository.Repository;
32 import org.apache.geronimo.system.serverinfo.ServerInfo;
33
34 /**
35  * @version $Rev: 156292 $ $Date: 2005-03-05 18:48:02 -0800 (Sat, 05 Mar 2005) $
36  */

37 public class ReadOnlyRepository implements Repository, GBeanLifecycle {
38     private static final Log log = LogFactory.getLog(ReadOnlyRepository.class);
39     private final URI JavaDoc root;
40     private final ServerInfo serverInfo;
41     private URI JavaDoc rootURI;
42
43     public ReadOnlyRepository(File JavaDoc root) {
44         this(root.toURI());
45     }
46
47     public ReadOnlyRepository(URI JavaDoc rootURI) {
48         this.root = null;
49         this.serverInfo = null;
50         this.rootURI = rootURI;
51     }
52
53     public ReadOnlyRepository(URI JavaDoc root, ServerInfo serverInfo) {
54         this.root = root;
55         this.serverInfo = serverInfo;
56     }
57
58     public boolean hasURI(URI JavaDoc uri) {
59         uri = rootURI.resolve(uri);
60         if ("file".equals(uri.getScheme())) {
61             File JavaDoc f = new File JavaDoc(uri);
62             return f.exists() && f.canRead();
63         } else {
64             try {
65                 uri.toURL().openStream().close();
66                 return true;
67             } catch (IOException JavaDoc e) {
68                 return false;
69             }
70         }
71     }
72
73     public URL JavaDoc getURL(URI JavaDoc uri) throws MalformedURLException JavaDoc {
74         return rootURI.resolve(uri).toURL();
75     }
76
77     public void doStart() throws Exception JavaDoc {
78         if (rootURI == null) {
79             rootURI = serverInfo.resolve(root);
80         }
81         log.info("Repository root is " + rootURI);
82     }
83
84     public void doStop() throws Exception JavaDoc {
85     }
86
87     public void doFail() {
88     }
89
90     public static final GBeanInfo GBEAN_INFO;
91
92     static {
93         GBeanInfoBuilder infoFactory = new GBeanInfoBuilder(ReadOnlyRepository.class);
94
95         infoFactory.addAttribute("root", URI JavaDoc.class, true);
96
97         infoFactory.addReference("ServerInfo", ServerInfo.class, "GBean");
98
99         infoFactory.addInterface(Repository.class);
100
101         infoFactory.setConstructor(new String JavaDoc[]{"root", "ServerInfo"});
102
103         GBEAN_INFO = infoFactory.getBeanInfo();
104     }
105
106     public static GBeanInfo getGBeanInfo() {
107         return GBEAN_INFO;
108     }
109 }
110
Popular Tags