Richard S. Hall | 930fecc | 2005-08-16 18:33:34 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2005 The Apache Software Foundation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | * |
| 16 | */ |
Richard S. Hall | 5a03159 | 2005-08-19 19:53:58 +0000 | [diff] [blame] | 17 | package org.apache.felix.shell.impl; |
Richard S. Hall | 930fecc | 2005-08-16 18:33:34 +0000 | [diff] [blame] | 18 | |
| 19 | import java.io.PrintStream; |
Richard S. Hall | 930fecc | 2005-08-16 18:33:34 +0000 | [diff] [blame] | 20 | import java.net.URL; |
| 21 | import java.util.StringTokenizer; |
| 22 | |
Richard S. Hall | 5a03159 | 2005-08-19 19:53:58 +0000 | [diff] [blame] | 23 | import org.apache.felix.shell.CdCommand; |
| 24 | import org.apache.felix.shell.Command; |
Richard S. Hall | 930fecc | 2005-08-16 18:33:34 +0000 | [diff] [blame] | 25 | import org.osgi.framework.*; |
| 26 | |
| 27 | public class InstallCommandImpl implements Command |
| 28 | { |
| 29 | private BundleContext m_context = null; |
| 30 | |
| 31 | public InstallCommandImpl(BundleContext context) |
| 32 | { |
| 33 | m_context = context; |
| 34 | } |
| 35 | |
| 36 | public String getName() |
| 37 | { |
| 38 | return "install"; |
| 39 | } |
| 40 | |
| 41 | public String getUsage() |
| 42 | { |
| 43 | return "install <URL> [<URL> ...]"; |
| 44 | } |
| 45 | |
| 46 | public String getShortDescription() |
| 47 | { |
| 48 | return "install bundle(s)."; |
| 49 | } |
| 50 | |
| 51 | public void execute(String s, PrintStream out, PrintStream err) |
| 52 | { |
| 53 | StringTokenizer st = new StringTokenizer(s, " "); |
| 54 | |
| 55 | // Ignore the command name. |
| 56 | st.nextToken(); |
| 57 | |
| 58 | // There should be at least one URL. |
| 59 | if (st.countTokens() >= 1) |
| 60 | { |
| 61 | while (st.hasMoreTokens()) |
| 62 | { |
| 63 | String location = st.nextToken().trim(); |
| 64 | install(location, out, err); |
| 65 | } |
| 66 | } |
| 67 | else |
| 68 | { |
| 69 | err.println("Incorrect number of arguments"); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | protected Bundle install(String location, PrintStream out, PrintStream err) |
| 74 | { |
| 75 | String abs = absoluteLocation(location); |
| 76 | if (abs == null) |
| 77 | { |
| 78 | err.println("Malformed location: " + location); |
| 79 | } |
| 80 | else |
| 81 | { |
| 82 | try |
| 83 | { |
| 84 | return m_context.installBundle(abs, null); |
| 85 | } |
| 86 | catch (IllegalStateException ex) |
| 87 | { |
| 88 | err.println(ex.toString()); |
| 89 | } |
| 90 | catch (BundleException ex) |
| 91 | { |
| 92 | if (ex.getNestedException() != null) |
| 93 | { |
| 94 | err.println(ex.getNestedException().toString()); |
| 95 | } |
| 96 | else |
| 97 | { |
| 98 | err.println(ex.toString()); |
| 99 | } |
| 100 | } |
| 101 | catch (Exception ex) |
| 102 | { |
| 103 | err.println(ex.toString()); |
| 104 | } |
| 105 | } |
| 106 | return null; |
| 107 | } |
| 108 | |
| 109 | private String absoluteLocation(String location) |
| 110 | { |
Richard S. Hall | 7c9da3d | 2006-02-24 20:09:28 +0000 | [diff] [blame] | 111 | String guess = location; |
| 112 | // If the location does not contain a ":", then try to |
| 113 | // add the base URL from the 'cd' command service. |
| 114 | if (location.indexOf(':') < 0) |
Richard S. Hall | 930fecc | 2005-08-16 18:33:34 +0000 | [diff] [blame] | 115 | { |
| 116 | // Try to create a valid URL using the base URL |
| 117 | // contained in the "cd" command service. |
| 118 | String baseURL = ""; |
| 119 | |
| 120 | try |
| 121 | { |
| 122 | // Get a reference to the "cd" command service. |
| 123 | ServiceReference ref = m_context.getServiceReference( |
Richard S. Hall | 5a03159 | 2005-08-19 19:53:58 +0000 | [diff] [blame] | 124 | org.apache.felix.shell.CdCommand.class.getName()); |
Richard S. Hall | 930fecc | 2005-08-16 18:33:34 +0000 | [diff] [blame] | 125 | |
| 126 | if (ref != null) |
| 127 | { |
| 128 | CdCommand cd = (CdCommand) m_context.getService(ref); |
| 129 | baseURL = cd.getBaseURL(); |
| 130 | baseURL = (baseURL == null) ? "" : baseURL; |
| 131 | m_context.ungetService(ref); |
| 132 | } |
| 133 | |
Richard S. Hall | 7c9da3d | 2006-02-24 20:09:28 +0000 | [diff] [blame] | 134 | String theURL = baseURL + guess; |
Richard S. Hall | 930fecc | 2005-08-16 18:33:34 +0000 | [diff] [blame] | 135 | new URL(theURL); |
Richard S. Hall | 930fecc | 2005-08-16 18:33:34 +0000 | [diff] [blame] | 136 | } |
| 137 | catch (Exception ex2) |
| 138 | { |
Richard S. Hall | 7c9da3d | 2006-02-24 20:09:28 +0000 | [diff] [blame] | 139 | // If that fails, then just return the original. |
| 140 | return location; |
Richard S. Hall | 930fecc | 2005-08-16 18:33:34 +0000 | [diff] [blame] | 141 | } |
Richard S. Hall | 7c9da3d | 2006-02-24 20:09:28 +0000 | [diff] [blame] | 142 | guess = baseURL + guess; |
Richard S. Hall | 930fecc | 2005-08-16 18:33:34 +0000 | [diff] [blame] | 143 | } |
Richard S. Hall | 7c9da3d | 2006-02-24 20:09:28 +0000 | [diff] [blame] | 144 | return guess; |
Richard S. Hall | 930fecc | 2005-08-16 18:33:34 +0000 | [diff] [blame] | 145 | } |
| 146 | } |