Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 1 | /* |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 2 | * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | * contributor license agreements. See the NOTICE file distributed with |
| 4 | * this work for additional information regarding copyright ownership. |
| 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | * (the "License"); you may not use this file except in compliance with |
| 7 | * the License. You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 17 | package org.apache.felix.shell.remote; |
| 18 | |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 19 | import org.apache.felix.shell.ShellService; |
Felix Meschberger | d7c324d | 2008-08-19 14:10:48 +0000 | [diff] [blame] | 20 | import org.osgi.framework.Bundle; |
| 21 | import org.osgi.framework.BundleContext; |
| 22 | import org.osgi.framework.Constants; |
| 23 | import org.osgi.framework.InvalidSyntaxException; |
| 24 | import org.osgi.framework.ServiceEvent; |
| 25 | import org.osgi.framework.ServiceListener; |
| 26 | import org.osgi.framework.ServiceReference; |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 27 | import org.osgi.service.log.LogService; |
Richard S. Hall | 55900ec | 2009-04-25 16:57:58 +0000 | [diff] [blame] | 28 | import org.osgi.util.tracker.ServiceTracker; |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 29 | |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 30 | /** |
| 31 | * Implements a mediator pattern class for services from the OSGi container. |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 32 | */ |
| 33 | class ServiceMediator |
| 34 | { |
Richard S. Hall | 55900ec | 2009-04-25 16:57:58 +0000 | [diff] [blame] | 35 | private final String m_bundleName; |
| 36 | private final long m_bundleId; |
| 37 | private final BundleContext m_context; |
| 38 | private final ServiceTracker m_logTracker; |
| 39 | private final ServiceTracker m_shellTracker; |
| 40 | |
| 41 | ServiceMediator(BundleContext context) |
| 42 | { |
| 43 | m_context = context; |
| 44 | m_bundleName = (m_context.getBundle().getSymbolicName() == null) |
| 45 | ? m_context.getBundle().getLocation() |
| 46 | : m_context.getBundle().getSymbolicName(); |
| 47 | m_bundleId = m_context.getBundle().getBundleId(); |
| 48 | ServiceTracker logTracker = null; |
| 49 | try |
| 50 | { |
| 51 | logTracker = new ServiceTracker(m_context, LogService.class.getName(), null); |
| 52 | logTracker.open(); |
| 53 | } |
| 54 | catch (Throwable ex) |
| 55 | { |
| 56 | // This means we don't have access to the log service package since it |
| 57 | // is optional, so don't track log services. |
| 58 | logTracker = null; |
| 59 | } |
| 60 | m_logTracker = logTracker; |
| 61 | m_shellTracker = new ServiceTracker(m_context, ShellService.class.getName(), null); |
| 62 | m_shellTracker.open(); |
| 63 | } |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 64 | |
| 65 | /** |
| 66 | * Returns a reference to the <tt>ShellService</tt> (Felix). |
| 67 | * |
| 68 | * @param wait time in milliseconds to wait for the reference if it isn't available. |
| 69 | * @return the reference to the <tt>ShellService</tt> as obtained from the OSGi service layer. |
| 70 | */ |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 71 | public ShellService getFelixShellService(long wait) |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 72 | { |
Richard S. Hall | 55900ec | 2009-04-25 16:57:58 +0000 | [diff] [blame] | 73 | ShellService shell = null; |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 74 | try |
| 75 | { |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 76 | if (wait < 0) |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 77 | { |
Richard S. Hall | 55900ec | 2009-04-25 16:57:58 +0000 | [diff] [blame] | 78 | shell = (ShellService) m_shellTracker.getService(); |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 79 | } |
Richard S. Hall | 55900ec | 2009-04-25 16:57:58 +0000 | [diff] [blame] | 80 | else |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 81 | { |
Richard S. Hall | 55900ec | 2009-04-25 16:57:58 +0000 | [diff] [blame] | 82 | shell = (ShellService) m_shellTracker.waitForService(wait); |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 83 | } |
| 84 | } |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 85 | catch (InterruptedException e) |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 86 | { |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 87 | e.printStackTrace(System.err); |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 88 | } |
| 89 | |
Richard S. Hall | 55900ec | 2009-04-25 16:57:58 +0000 | [diff] [blame] | 90 | return shell; |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 91 | }//getFelixShellService |
| 92 | |
Richard S. Hall | 55900ec | 2009-04-25 16:57:58 +0000 | [diff] [blame] | 93 | public Object getLogServiceLatch(long wait) |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 94 | { |
Richard S. Hall | 55900ec | 2009-04-25 16:57:58 +0000 | [diff] [blame] | 95 | Object log = null; |
| 96 | if (m_logTracker != null) |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 97 | { |
Richard S. Hall | 55900ec | 2009-04-25 16:57:58 +0000 | [diff] [blame] | 98 | try |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 99 | { |
Richard S. Hall | 55900ec | 2009-04-25 16:57:58 +0000 | [diff] [blame] | 100 | if (wait < 0) |
| 101 | { |
| 102 | log = m_logTracker.getService(); |
| 103 | } |
| 104 | else |
| 105 | { |
| 106 | log = m_logTracker.waitForService(wait); |
| 107 | } |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 108 | } |
Richard S. Hall | 55900ec | 2009-04-25 16:57:58 +0000 | [diff] [blame] | 109 | catch (InterruptedException e) |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 110 | { |
Richard S. Hall | 55900ec | 2009-04-25 16:57:58 +0000 | [diff] [blame] | 111 | e.printStackTrace(System.err); |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 112 | } |
| 113 | } |
Richard S. Hall | 55900ec | 2009-04-25 16:57:58 +0000 | [diff] [blame] | 114 | return log; |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 115 | }//getLogService |
| 116 | |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 117 | public void info(String msg) |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 118 | { |
Richard S. Hall | 55900ec | 2009-04-25 16:57:58 +0000 | [diff] [blame] | 119 | Object log = getLogServiceLatch(NO_WAIT); |
| 120 | if (log != null) |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 121 | { |
Richard S. Hall | 55900ec | 2009-04-25 16:57:58 +0000 | [diff] [blame] | 122 | ((LogService) log).log(LogService.LOG_INFO, msg); |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 123 | } |
| 124 | else |
| 125 | { |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 126 | sysout(msg); |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 127 | } |
| 128 | }//info |
| 129 | |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 130 | public void error(String msg, Throwable t) |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 131 | { |
Richard S. Hall | 55900ec | 2009-04-25 16:57:58 +0000 | [diff] [blame] | 132 | Object log = getLogServiceLatch(NO_WAIT); |
| 133 | if (log != null) |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 134 | { |
Richard S. Hall | 55900ec | 2009-04-25 16:57:58 +0000 | [diff] [blame] | 135 | ((LogService) log).log(LogService.LOG_ERROR, msg); |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 136 | } |
| 137 | else |
| 138 | { |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 139 | syserr(msg, t); |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 140 | } |
| 141 | }//error |
| 142 | |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 143 | public void error(String msg) |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 144 | { |
Richard S. Hall | 55900ec | 2009-04-25 16:57:58 +0000 | [diff] [blame] | 145 | Object log = getLogServiceLatch(NO_WAIT); |
| 146 | if (log != null) |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 147 | { |
Richard S. Hall | 55900ec | 2009-04-25 16:57:58 +0000 | [diff] [blame] | 148 | ((LogService) log).log(LogService.LOG_ERROR, msg); |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 149 | } |
| 150 | else |
| 151 | { |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 152 | syserr(msg, null); |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 153 | } |
| 154 | }//error |
| 155 | |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 156 | public void debug(String msg) |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 157 | { |
Richard S. Hall | 55900ec | 2009-04-25 16:57:58 +0000 | [diff] [blame] | 158 | Object log = getLogServiceLatch(NO_WAIT); |
| 159 | if (log != null) |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 160 | { |
Richard S. Hall | 55900ec | 2009-04-25 16:57:58 +0000 | [diff] [blame] | 161 | ((LogService) log).log(LogService.LOG_DEBUG, msg); |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 162 | } |
| 163 | else |
| 164 | { |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 165 | sysout(msg); |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 166 | } |
| 167 | }//debug |
| 168 | |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 169 | public void warn(String msg) |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 170 | { |
Richard S. Hall | 55900ec | 2009-04-25 16:57:58 +0000 | [diff] [blame] | 171 | Object log = getLogServiceLatch(NO_WAIT); |
| 172 | if (log != null) |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 173 | { |
Richard S. Hall | 55900ec | 2009-04-25 16:57:58 +0000 | [diff] [blame] | 174 | ((LogService) log).log(LogService.LOG_WARNING, msg); |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 175 | } |
| 176 | else |
| 177 | { |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 178 | syserr(msg, null); |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 179 | } |
| 180 | }//warn |
| 181 | |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 182 | private void sysout(String msg) |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 183 | { |
| 184 | //Assemble String |
| 185 | StringBuffer sbuf = new StringBuffer(); |
Richard S. Hall | 55900ec | 2009-04-25 16:57:58 +0000 | [diff] [blame] | 186 | sbuf.append(m_bundleName); |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 187 | sbuf.append(" ["); |
Richard S. Hall | 55900ec | 2009-04-25 16:57:58 +0000 | [diff] [blame] | 188 | sbuf.append(m_bundleId); |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 189 | sbuf.append("] "); |
| 190 | sbuf.append(msg); |
| 191 | System.out.println(sbuf.toString()); |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 192 | }//sysout |
| 193 | |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 194 | private void syserr(String msg, Throwable t) |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 195 | { |
| 196 | //Assemble String |
| 197 | StringBuffer sbuf = new StringBuffer(); |
Richard S. Hall | 55900ec | 2009-04-25 16:57:58 +0000 | [diff] [blame] | 198 | sbuf.append(m_bundleName); |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 199 | sbuf.append(" ["); |
Richard S. Hall | 55900ec | 2009-04-25 16:57:58 +0000 | [diff] [blame] | 200 | sbuf.append(m_bundleId); |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 201 | sbuf.append("] "); |
| 202 | sbuf.append(msg); |
| 203 | System.err.println(sbuf.toString()); |
| 204 | if (t != null) |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 205 | { |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 206 | t.printStackTrace(System.err); |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 207 | } |
| 208 | }//logToSystem |
| 209 | |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 210 | /** |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 211 | * Deactivates this mediator, nulling out all references. |
| 212 | * If called when the bundle is stopped, the framework should actually take |
| 213 | * care of unregistering the <tt>ServiceListener</tt>. |
| 214 | */ |
| 215 | public void deactivate() |
| 216 | { |
Richard S. Hall | 55900ec | 2009-04-25 16:57:58 +0000 | [diff] [blame] | 217 | if (m_logTracker != null) |
| 218 | { |
| 219 | m_logTracker.close(); |
| 220 | } |
| 221 | m_shellTracker.close(); |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 222 | }//deactivate |
| 223 | |
Richard S. Hall | 55900ec | 2009-04-25 16:57:58 +0000 | [diff] [blame] | 224 | public static long WAIT_UNLIMITED = 0; |
| 225 | public static long NO_WAIT = -1; |
| 226 | }//class ServiceMediator |