1 36 package org.ungoverned.oscar.util; 37 38 import java.io.BufferedInputStream ; 39 import java.io.File ; 40 import java.io.InputStream ; 41 42 import org.ungoverned.oscar.*; 43 44 89 public class DefaultBundleCache implements BundleCache 90 { 91 public static final String CACHE_BUFSIZE_PROP = "oscar.cache.bufsize"; 92 public static final String CACHE_DIR_PROP = "oscar.cache.dir"; 93 public static final String CACHE_PROFILE_DIR_PROP = "oscar.cache.profiledir"; 94 public static final String CACHE_PROFILE_PROP = "oscar.cache.profile"; 95 96 protected static transient int BUFSIZE = 4096; 97 protected static transient final String CACHE_DIR_NAME = ".oscar"; 98 protected static transient final String BUNDLE_DIR_PREFIX = "bundle"; 99 100 private Oscar m_oscar = null; 101 private File m_profileDir = null; 102 private BundleArchive[] m_archives = null; 103 104 public DefaultBundleCache() 105 { 106 } 107 108 public void initialize(Oscar oscar) throws Exception 109 { 110 m_oscar = oscar; 112 113 try 115 { 116 String sBufSize = 117 m_oscar.getConfigProperty(CACHE_BUFSIZE_PROP); 118 if (sBufSize != null) 119 { 120 BUFSIZE = Integer.parseInt(sBufSize); 121 } 122 } 123 catch (NumberFormatException ne) 124 { 125 } 127 128 String profileDirStr = 130 m_oscar.getConfigProperty(CACHE_PROFILE_DIR_PROP); 131 if (profileDirStr != null) 132 { 133 m_profileDir = new File (profileDirStr); 134 } 135 else 136 { 137 141 String cacheDirStr = 144 m_oscar.getConfigProperty(CACHE_DIR_PROP); 145 if (cacheDirStr == null) 146 { 147 cacheDirStr = System.getProperty("user.home"); 150 cacheDirStr = cacheDirStr.endsWith(File.separator) 151 ? cacheDirStr : cacheDirStr + File.separator; 152 cacheDirStr = cacheDirStr + CACHE_DIR_NAME; 153 } 154 155 String profileName = 157 m_oscar.getConfigProperty(CACHE_PROFILE_PROP); 158 if (profileName == null) 159 { 160 throw new IllegalArgumentException ( 161 "No profile name or directory has been specified."); 162 } 163 else if (profileName.indexOf(File.separator) >= 0) 165 { 166 throw new IllegalArgumentException ( 167 "The profile name cannot contain the file separator character."); 168 } 169 170 m_profileDir = new File (cacheDirStr, profileName); 171 } 172 173 if (!m_profileDir.exists()) 175 { 176 if (!m_profileDir.mkdirs()) 177 { 178 Oscar.error("Unable to create directory: " + m_profileDir); 179 throw new RuntimeException ("Unable to create profile directory."); 180 } 181 } 182 183 File [] children = m_profileDir.listFiles(); 186 int count = 0; 187 for (int i = 0; (children != null) && (i < children.length); i++) 188 { 189 if (children[i].getName().startsWith(BUNDLE_DIR_PREFIX)) 191 { 192 count++; 193 } 194 } 195 m_archives = new BundleArchive[count]; 196 count = 0; 197 for (int i = 0; (children != null) && (i < children.length); i++) 198 { 199 if (children[i].getName().startsWith(BUNDLE_DIR_PREFIX)) 201 { 202 String id = children[i].getName().substring(BUNDLE_DIR_PREFIX.length()); 203 m_archives[count] = new DefaultBundleArchive( 204 children[i], Long.parseLong(id)); 205 count++; 206 } 207 } 208 } 209 210 public BundleArchive[] getArchives() 211 throws Exception 212 { 213 return m_archives; 214 } 215 216 public BundleArchive getArchive(long id) 217 throws Exception 218 { 219 for (int i = 0; i < m_archives.length; i++) 220 { 221 if (m_archives[i].getId() == id) 222 { 223 return m_archives[i]; 224 } 225 } 226 return null; 227 } 228 229 public BundleArchive create(long id, String location, InputStream is) 230 throws Exception 231 { 232 File bundleDir = new File (m_profileDir, "bundle" + id); 234 235 try 236 { 237 is = new BufferedInputStream (is, DefaultBundleCache.BUFSIZE); 239 BundleArchive ba = new DefaultBundleArchive(bundleDir, id, location, is); 241 BundleArchive[] bas = new BundleArchive[m_archives.length + 1]; 243 System.arraycopy(m_archives, 0, bas, 0, m_archives.length); 244 bas[m_archives.length] = ba; 245 m_archives = bas; 246 return ba; 247 } 248 finally 249 { 250 if (is != null) is.close(); 251 } 252 } 253 254 public void update(BundleArchive ba, InputStream is) 255 throws Exception 256 { 257 try 258 { 259 is = new BufferedInputStream (is, DefaultBundleCache.BUFSIZE); 261 ((DefaultBundleArchive) ba).update(is); 263 } 264 finally 265 { 266 if (is != null) is.close(); 267 } 268 } 269 270 public void purge(BundleArchive ba) 271 throws Exception 272 { 273 ((DefaultBundleArchive) ba).purge(); 274 } 275 276 public void remove(BundleArchive ba) 277 throws Exception 278 { 279 ((DefaultBundleArchive) ba).remove(); 280 } 281 } | Popular Tags |