1 17 18 package org.apache.geronimo.system.repository; 19 20 import java.io.File ; 21 import java.io.IOException ; 22 import java.net.MalformedURLException ; 23 import java.net.URI ; 24 import java.net.URL ; 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 37 public class ReadOnlyRepository implements Repository, GBeanLifecycle { 38 private static final Log log = LogFactory.getLog(ReadOnlyRepository.class); 39 private final URI root; 40 private final ServerInfo serverInfo; 41 private URI rootURI; 42 43 public ReadOnlyRepository(File root) { 44 this(root.toURI()); 45 } 46 47 public ReadOnlyRepository(URI rootURI) { 48 this.root = null; 49 this.serverInfo = null; 50 this.rootURI = rootURI; 51 } 52 53 public ReadOnlyRepository(URI root, ServerInfo serverInfo) { 54 this.root = root; 55 this.serverInfo = serverInfo; 56 } 57 58 public boolean hasURI(URI uri) { 59 uri = rootURI.resolve(uri); 60 if ("file".equals(uri.getScheme())) { 61 File f = new File (uri); 62 return f.exists() && f.canRead(); 63 } else { 64 try { 65 uri.toURL().openStream().close(); 66 return true; 67 } catch (IOException e) { 68 return false; 69 } 70 } 71 } 72 73 public URL getURL(URI uri) throws MalformedURLException { 74 return rootURI.resolve(uri).toURL(); 75 } 76 77 public void doStart() throws Exception { 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 { 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 .class, true); 96 97 infoFactory.addReference("ServerInfo", ServerInfo.class, "GBean"); 98 99 infoFactory.addInterface(Repository.class); 100 101 infoFactory.setConstructor(new String []{"root", "ServerInfo"}); 102 103 GBEAN_INFO = infoFactory.getBeanInfo(); 104 } 105 106 public static GBeanInfo getGBeanInfo() { 107 return GBEAN_INFO; 108 } 109 } 110 | Popular Tags |