001/* 002 * (C) Copyright 2006-2011 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * bstefanescu 018 * 019 * $Id$ 020 */ 021 022package org.nuxeo.osgi.application; 023 024import java.io.BufferedReader; 025import java.io.BufferedWriter; 026import java.io.File; 027import java.io.FileReader; 028import java.io.FileWriter; 029import java.io.IOException; 030import java.util.ArrayList; 031import java.util.List; 032 033import org.nuxeo.osgi.BundleFile; 034import org.nuxeo.osgi.DirectoryBundleFile; 035import org.nuxeo.osgi.JarBundleFile; 036import org.osgi.framework.BundleException; 037 038/** 039 * @author <a href="mailto:[email protected]">Bogdan Stefanescu</a> 040 */ 041public class ClassPath implements ClassPathScanner.Callback { 042 043 protected final List<BundleFile> bundles; 044 045 protected final List<BundleFile> jars; 046 047 protected final List<BundleFile> nestedJars; 048 049 protected final SharedClassLoader loader; 050 051 protected final File nestedJARsDir; 052 053 public ClassPath(SharedClassLoader loader, File nestedJARsDir) { 054 bundles = new ArrayList<BundleFile>(); 055 jars = new ArrayList<BundleFile>(); 056 nestedJars = new ArrayList<BundleFile>(); 057 this.loader = loader; 058 this.nestedJARsDir = nestedJARsDir; 059 nestedJARsDir.mkdirs(); 060 } 061 062 public List<BundleFile> getBundles() { 063 return bundles; 064 } 065 066 public List<BundleFile> getJars() { 067 return jars; 068 } 069 070 public List<BundleFile> getNestedJars() { 071 return nestedJars; 072 } 073 074 public void scan(List<File> files, boolean scanForNestedJARs, String[] blacklist) { 075 new ClassPathScanner(this, scanForNestedJARs, blacklist).scan(files); 076 } 077 078 @Override 079 public File handleBundle(BundleFile bf) { 080 bundles.add(bf); 081 loader.addURL(bf.getURL()); 082 return nestedJARsDir; 083 } 084 085 @Override 086 public File handleJar(BundleFile bf) { 087 jars.add(bf); 088 loader.addURL(bf.getURL()); 089 return nestedJARsDir; 090 } 091 092 @Override 093 public void handleNestedJar(BundleFile bf) { 094 nestedJars.add(bf); 095 loader.addURL(bf.getURL()); 096 } 097 098 public void store(File file) throws IOException { 099 BufferedWriter writer = null; 100 try { 101 writer = new BufferedWriter(new FileWriter(file)); 102 for (BundleFile bf : bundles) { 103 writer.append(bf.getFile().getAbsolutePath()); 104 writer.newLine(); 105 } 106 writer.append("#"); 107 writer.newLine(); 108 for (BundleFile bf : jars) { 109 writer.append(bf.getFile().getAbsolutePath()); 110 writer.newLine(); 111 } 112 writer.append("#"); 113 writer.newLine(); 114 for (BundleFile bf : nestedJars) { 115 writer.append(bf.getFile().getAbsolutePath()); 116 writer.newLine(); 117 } 118 } finally { 119 if (writer != null) { 120 writer.close(); 121 } 122 } 123 } 124 125 public void restore(File file) throws IOException { 126 BufferedReader reader = null; 127 try { 128 reader = new BufferedReader(new FileReader(file)); 129 List<BundleFile> list = bundles; 130 String line = null; 131 while (true) { 132 line = reader.readLine(); 133 if (line == null) { 134 break; 135 } 136 if (line.startsWith("#")) { 137 if (list == bundles) { 138 list = jars; 139 } else if (list == jars) { 140 list = nestedJars; 141 } 142 continue; 143 } 144 BundleFile bf = null; 145 File f = new File(line.trim()); 146 if (f.isDirectory()) { 147 bf = new DirectoryBundleFile(f); 148 } else { 149 bf = new JarBundleFile(f); 150 } 151 loader.addURL(bf.getURL()); 152 list.add(bf); 153 } 154 } finally { 155 if (reader != null) { 156 reader.close(); 157 } 158 } 159 } 160 161}