Richard S. Hall | af656a0 | 2009-06-11 16:07:20 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
| 19 | package aQute.osgi.launcher; |
| 20 | |
| 21 | import java.io.*; |
| 22 | import java.lang.reflect.*; |
| 23 | import java.net.*; |
| 24 | import java.util.*; |
| 25 | |
| 26 | import org.osgi.framework.*; |
| 27 | import org.osgi.service.command.*; |
| 28 | |
| 29 | import aQute.shell.console.*; |
| 30 | import aQute.shell.osgi.*; |
| 31 | import aQute.threadio.*; |
| 32 | |
| 33 | public class Launcher { |
| 34 | static List<URL> classpath = new ArrayList<URL>(); |
| 35 | static File cwd = new File("").getAbsoluteFile(); |
| 36 | |
| 37 | public static void main(String args[]) throws Exception { |
| 38 | StringBuffer sb = new StringBuffer(); |
| 39 | String framework = null; |
| 40 | PrintStream out = System.out; |
| 41 | InputStream in = System.in; |
| 42 | boolean console = false; |
| 43 | |
| 44 | for (int i = 0; i < args.length; i++) { |
| 45 | String arg = args[i]; |
| 46 | if (arg.equals("-f")) { |
| 47 | framework = args[++i]; |
| 48 | } else if (arg.equals("-cp") || arg.equals("-classpath")) { |
| 49 | classpath(args[++i]); |
| 50 | } else if (arg.equals("-console")) { |
| 51 | console = true; |
| 52 | } else if (arg.equals("-i")) { |
| 53 | in = new FileInputStream(args[++i]); |
| 54 | } else if (arg.equals("-o")) { |
| 55 | out = new PrintStream(new FileOutputStream(args[++i])); |
| 56 | } else { |
| 57 | sb.append(' '); |
| 58 | sb.append(arg); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | if (framework == null) { |
| 63 | System.err.println("No framework set"); |
| 64 | System.exit(1); |
| 65 | } |
| 66 | |
| 67 | ThreadIOImpl threadio = new ThreadIOImpl(); |
| 68 | threadio.start(); |
| 69 | URL[] urls = classpath.toArray(new URL[classpath.size()]); |
| 70 | URLClassLoader urlcl = new URLClassLoader(urls, Launcher.class.getClassLoader()); |
| 71 | Class<?> fw = urlcl.loadClass(framework); |
| 72 | |
| 73 | Constructor<?> c = fw.getConstructor(Map.class, List.class ); |
| 74 | Properties p = new Properties( System.getProperties()); |
| 75 | p.setProperty("felix.cache.profile", "default"); |
| 76 | p.setProperty("felix.embedded.execution", "true"); |
| 77 | Bundle bundle = (Bundle) c.newInstance(p,null); |
| 78 | |
| 79 | OSGiShell shell = new OSGiShell(); |
| 80 | shell.setThreadio(threadio); |
| 81 | shell.setBundle(bundle); |
| 82 | shell.start(); |
| 83 | |
| 84 | |
| 85 | CommandSession session = shell.createSession(in, out, |
| 86 | System.err); |
| 87 | session.put("shell", shell); |
| 88 | session.put("threadio", threadio); |
| 89 | |
| 90 | session.execute(sb); |
| 91 | out.flush(); |
| 92 | |
| 93 | if ( bundle.getState() == Bundle.ACTIVE ) { |
| 94 | bundle.getBundleContext().registerService(CommandProcessor.class.getName(), shell, null ); |
| 95 | } |
| 96 | if ( console ) { |
| 97 | Console cons = new Console(); |
| 98 | cons.setSession(session); |
| 99 | cons.run(); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | |
| 104 | private static void classpath(String string) throws MalformedURLException { |
| 105 | StringTokenizer st = new StringTokenizer(string, File.pathSeparator); |
| 106 | while (st.hasMoreTokens()) { |
| 107 | String part = st.nextToken(); |
| 108 | if ( part.equals(".")) |
| 109 | classpath.add( cwd.toURL() ); |
| 110 | |
| 111 | File f = new File(part); |
| 112 | if ( ! f.isAbsolute() ) { |
| 113 | f = new File( cwd, part ); |
| 114 | } |
| 115 | if ( f.exists() ) |
| 116 | classpath.add( f.toURL()); |
| 117 | else |
| 118 | System.err.println("Can not find " + part ); |
| 119 | } |
| 120 | } |
| 121 | } |