1 36 package org.ungoverned.moduleloader; 37 38 import java.io.ByteArrayInputStream ; 39 import java.io.IOException ; 40 import java.io.InputStream ; 41 import java.net.URL ; 42 import java.net.URLConnection ; 43 import java.security.Permission ; 44 45 class ModuleURLConnection extends URLConnection 46 { 47 private ModuleManager m_mgr = null; 48 private int m_contentLength; 49 private long m_contentTime; 50 private String m_contentType; 51 private InputStream m_is; 52 53 public ModuleURLConnection(ModuleManager mgr, URL url) 54 { 55 super(url); 56 m_mgr = mgr; 57 } 58 59 public void connect() throws IOException 60 { 61 if (!connected) 62 { 63 Module module = m_mgr.getModule(url.getHost()); 66 if (module == null) 67 { 68 throw new IOException ("Unable to find bundle's module."); 69 } 70 71 String resource = url.getFile(); 72 if (resource == null) 73 { 74 throw new IOException ("Unable to find resource: " + url.toString()); 75 } 76 if (resource.startsWith("/")) 77 { 78 resource = resource.substring(1); 79 } 80 int rsIdx = -1; 81 try 82 { 83 rsIdx = Integer.parseInt(resource.substring(0, resource.indexOf("/"))); 84 } 85 catch (NumberFormatException ex) 86 { 87 new IOException ("Error parsing resource index."); 88 } 89 resource = resource.substring(resource.indexOf("/") + 1); 90 91 byte[] bytes = null; 93 ResourceSource[] resSources = module.getResourceSources(); 94 if ((resSources != null) && (rsIdx < resSources.length)) 95 { 96 if (resSources[rsIdx].hasResource(resource)) 97 { 98 bytes = resSources[rsIdx].getBytes(resource); 99 } 100 } 101 102 if (bytes == null) 103 { 104 throw new IOException ("Unable to find resource: " + url.toString()); 105 } 106 107 m_is = new ByteArrayInputStream (bytes); 108 m_contentLength = bytes.length; 109 m_contentTime = 0L; m_contentType = URLConnection.guessContentTypeFromName(resource); 111 connected = true; 112 } 113 } 114 115 public InputStream getInputStream() 116 throws IOException 117 { 118 if (!connected) 119 { 120 connect(); 121 } 122 return m_is; 123 } 124 125 public int getContentLength() 126 { 127 if (!connected) 128 { 129 try { 130 connect(); 131 } catch(IOException ex) { 132 return -1; 133 } 134 } 135 return m_contentLength; 136 } 137 138 public long getLastModified() 139 { 140 if (!connected) 141 { 142 try { 143 connect(); 144 } catch(IOException ex) { 145 return 0; 146 } 147 } 148 if (m_contentTime != -1L) 149 { 150 return m_contentTime; 151 } 152 else 153 { 154 return 0L; 155 } 156 } 157 158 public String getContentType() 159 { 160 if (!connected) 161 { 162 try { 163 connect(); 164 } catch(IOException ex) { 165 return null; 166 } 167 } 168 return m_contentType; 169 } 170 171 public Permission getPermission() 172 { 173 return null; 179 } 180 } | Popular Tags |