Richard S. Hall | fcaa2d4 | 2006-09-28 19:56:03 +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 |
Richard S. Hall | 930fecc | 2005-08-16 18:33:34 +0000 | [diff] [blame] | 9 | * |
Richard S. Hall | fcaa2d4 | 2006-09-28 19:56:03 +0000 | [diff] [blame] | 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
Richard S. Hall | 930fecc | 2005-08-16 18:33:34 +0000 | [diff] [blame] | 11 | * |
Richard S. Hall | fcaa2d4 | 2006-09-28 19:56:03 +0000 | [diff] [blame] | 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. |
Richard S. Hall | 930fecc | 2005-08-16 18:33:34 +0000 | [diff] [blame] | 18 | */ |
Richard S. Hall | 7164494 | 2006-02-08 14:29:10 +0000 | [diff] [blame] | 19 | package org.apache.felix.shell.tui; |
Richard S. Hall | 930fecc | 2005-08-16 18:33:34 +0000 | [diff] [blame] | 20 | |
| 21 | import java.io.*; |
| 22 | |
Richard S. Hall | 5a03159 | 2005-08-19 19:53:58 +0000 | [diff] [blame] | 23 | import org.apache.felix.shell.ShellService; |
Richard S. Hall | 930fecc | 2005-08-16 18:33:34 +0000 | [diff] [blame] | 24 | import org.osgi.framework.*; |
| 25 | |
| 26 | public class Activator implements BundleActivator |
| 27 | { |
Richard S. Hall | 5981531 | 2006-03-17 15:57:03 +0000 | [diff] [blame] | 28 | private BundleContext m_context = null; |
Richard S. Hall | 9203b4c | 2008-09-23 14:03:15 +0000 | [diff] [blame] | 29 | private volatile ShellTuiRunnable m_runnable = null; |
| 30 | private volatile Thread m_thread = null; |
Richard S. Hall | 5981531 | 2006-03-17 15:57:03 +0000 | [diff] [blame] | 31 | private ServiceReference m_shellRef = null; |
| 32 | private ShellService m_shell = null; |
Richard S. Hall | 930fecc | 2005-08-16 18:33:34 +0000 | [diff] [blame] | 33 | |
| 34 | public void start(BundleContext context) |
| 35 | { |
| 36 | m_context = context; |
| 37 | |
Richard S. Hall | 5a03159 | 2005-08-19 19:53:58 +0000 | [diff] [blame] | 38 | // Listen for registering/unregistering impl service. |
Richard S. Hall | 930fecc | 2005-08-16 18:33:34 +0000 | [diff] [blame] | 39 | ServiceListener sl = new ServiceListener() { |
| 40 | public void serviceChanged(ServiceEvent event) |
| 41 | { |
| 42 | synchronized (Activator.this) |
| 43 | { |
Richard S. Hall | 930fecc | 2005-08-16 18:33:34 +0000 | [diff] [blame] | 44 | // Initialize the service if we don't have one. |
Richard S. Hall | cd48fc6 | 2009-05-18 15:22:37 +0000 | [diff] [blame] | 45 | if ((event.getType() == ServiceEvent.REGISTERED) |
Richard S. Hall | 930fecc | 2005-08-16 18:33:34 +0000 | [diff] [blame] | 46 | && (m_shellRef == null)) |
| 47 | { |
| 48 | initializeService(); |
| 49 | } |
| 50 | // Unget the service if it is unregistering. |
| 51 | else if ((event.getType() == ServiceEvent.UNREGISTERING) |
| 52 | && event.getServiceReference().equals(m_shellRef)) |
| 53 | { |
| 54 | m_context.ungetService(m_shellRef); |
| 55 | m_shellRef = null; |
| 56 | m_shell = null; |
| 57 | // Try to get another service. |
| 58 | initializeService(); |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | }; |
Richard S. Hall | 5981531 | 2006-03-17 15:57:03 +0000 | [diff] [blame] | 63 | try |
| 64 | { |
Richard S. Hall | 930fecc | 2005-08-16 18:33:34 +0000 | [diff] [blame] | 65 | m_context.addServiceListener(sl, |
| 66 | "(objectClass=" |
Richard S. Hall | 5a03159 | 2005-08-19 19:53:58 +0000 | [diff] [blame] | 67 | + org.apache.felix.shell.ShellService.class.getName() |
Richard S. Hall | 930fecc | 2005-08-16 18:33:34 +0000 | [diff] [blame] | 68 | + ")"); |
Richard S. Hall | 5981531 | 2006-03-17 15:57:03 +0000 | [diff] [blame] | 69 | } |
| 70 | catch (InvalidSyntaxException ex) |
| 71 | { |
| 72 | System.err.println("ShellTui: Cannot add service listener."); |
| 73 | System.err.println("ShellTui: " + ex); |
Richard S. Hall | 930fecc | 2005-08-16 18:33:34 +0000 | [diff] [blame] | 74 | } |
| 75 | |
Richard S. Hall | 5a03159 | 2005-08-19 19:53:58 +0000 | [diff] [blame] | 76 | // Now try to manually initialize the impl service |
Richard S. Hall | 930fecc | 2005-08-16 18:33:34 +0000 | [diff] [blame] | 77 | // since one might already be available. |
| 78 | initializeService(); |
| 79 | |
Karl Pauls | 957ca19 | 2008-04-21 19:55:56 +0000 | [diff] [blame] | 80 | // Start impl thread. |
| 81 | m_thread = new Thread( |
| 82 | m_runnable = new ShellTuiRunnable(), |
| 83 | "Felix Shell TUI"); |
Richard S. Hall | 5981531 | 2006-03-17 15:57:03 +0000 | [diff] [blame] | 84 | m_thread.start(); |
Richard S. Hall | 930fecc | 2005-08-16 18:33:34 +0000 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | private synchronized void initializeService() |
| 88 | { |
Richard S. Hall | cd48fc6 | 2009-05-18 15:22:37 +0000 | [diff] [blame] | 89 | if (m_shell == null) |
Richard S. Hall | 5981531 | 2006-03-17 15:57:03 +0000 | [diff] [blame] | 90 | { |
Richard S. Hall | cd48fc6 | 2009-05-18 15:22:37 +0000 | [diff] [blame] | 91 | m_shellRef = m_context.getServiceReference( |
| 92 | org.apache.felix.shell.ShellService.class.getName()); |
| 93 | if (m_shellRef != null) |
| 94 | { |
| 95 | m_shell = (ShellService) m_context.getService(m_shellRef); |
| 96 | } |
Richard S. Hall | 5981531 | 2006-03-17 15:57:03 +0000 | [diff] [blame] | 97 | } |
Richard S. Hall | 930fecc | 2005-08-16 18:33:34 +0000 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | public void stop(BundleContext context) |
| 101 | { |
Karl Pauls | 957ca19 | 2008-04-21 19:55:56 +0000 | [diff] [blame] | 102 | if (m_runnable != null) |
Richard S. Hall | 930fecc | 2005-08-16 18:33:34 +0000 | [diff] [blame] | 103 | { |
Karl Pauls | 957ca19 | 2008-04-21 19:55:56 +0000 | [diff] [blame] | 104 | m_runnable.stop(); |
Richard S. Hall | 930fecc | 2005-08-16 18:33:34 +0000 | [diff] [blame] | 105 | } |
| 106 | } |
| 107 | |
| 108 | private class ShellTuiRunnable implements Runnable |
| 109 | { |
Richard S. Hall | 3280251 | 2008-11-21 22:10:58 +0000 | [diff] [blame] | 110 | private volatile boolean m_stop = false; |
Richard S. Hall | 734d712 | 2008-11-21 21:56:55 +0000 | [diff] [blame] | 111 | |
Richard S. Hall | 930fecc | 2005-08-16 18:33:34 +0000 | [diff] [blame] | 112 | public void stop() |
| 113 | { |
Richard S. Hall | 3280251 | 2008-11-21 22:10:58 +0000 | [diff] [blame] | 114 | m_stop = true; |
Richard S. Hall | 930fecc | 2005-08-16 18:33:34 +0000 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | public void run() |
| 118 | { |
| 119 | String line = null; |
| 120 | BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); |
Karl Pauls | 957ca19 | 2008-04-21 19:55:56 +0000 | [diff] [blame] | 121 | |
Richard S. Hall | 3280251 | 2008-11-21 22:10:58 +0000 | [diff] [blame] | 122 | try |
| 123 | { |
Richard S. Hall | cd48fc6 | 2009-05-18 15:22:37 +0000 | [diff] [blame] | 124 | boolean needPrompt = true; |
| 125 | int available; |
| 126 | while (!m_stop) |
Richard S. Hall | 5981531 | 2006-03-17 15:57:03 +0000 | [diff] [blame] | 127 | { |
Richard S. Hall | cd48fc6 | 2009-05-18 15:22:37 +0000 | [diff] [blame] | 128 | if (needPrompt) |
| 129 | { |
| 130 | System.out.print("-> "); |
| 131 | needPrompt = false; |
| 132 | } |
| 133 | |
| 134 | available = System.in.available(); |
| 135 | |
| 136 | if (available == 0) |
| 137 | { |
| 138 | try |
| 139 | { |
| 140 | Thread.sleep(200); |
| 141 | } |
| 142 | catch (InterruptedException ex) |
| 143 | { |
| 144 | // No one should be interrupting this thread, so |
| 145 | // ignore it. |
| 146 | } |
| 147 | continue; |
| 148 | } |
| 149 | |
Richard S. Hall | 930fecc | 2005-08-16 18:33:34 +0000 | [diff] [blame] | 150 | line = in.readLine(); |
Richard S. Hall | 9203b4c | 2008-09-23 14:03:15 +0000 | [diff] [blame] | 151 | if (line == null) |
| 152 | { |
| 153 | System.err.println("ShellTUI: No standard input...exiting."); |
| 154 | break; |
| 155 | } |
Richard S. Hall | cd48fc6 | 2009-05-18 15:22:37 +0000 | [diff] [blame] | 156 | needPrompt = true; |
Richard S. Hall | 930fecc | 2005-08-16 18:33:34 +0000 | [diff] [blame] | 157 | |
Richard S. Hall | 930fecc | 2005-08-16 18:33:34 +0000 | [diff] [blame] | 158 | line = line.trim(); |
Richard S. Hall | 930fecc | 2005-08-16 18:33:34 +0000 | [diff] [blame] | 159 | if (line.length() == 0) |
| 160 | { |
| 161 | continue; |
| 162 | } |
| 163 | |
Richard S. Hall | cd48fc6 | 2009-05-18 15:22:37 +0000 | [diff] [blame] | 164 | synchronized (Activator.this) |
Richard S. Hall | 5981531 | 2006-03-17 15:57:03 +0000 | [diff] [blame] | 165 | { |
Richard S. Hall | cd48fc6 | 2009-05-18 15:22:37 +0000 | [diff] [blame] | 166 | if (m_shell == null) |
| 167 | { |
| 168 | System.out.println("No impl service available."); |
| 169 | continue; |
| 170 | } |
| 171 | |
| 172 | try |
| 173 | { |
| 174 | m_shell.executeCommand(line, System.out, System.err); |
| 175 | } |
| 176 | catch (Exception ex) |
| 177 | { |
| 178 | System.err.println("ShellTUI: " + ex); |
| 179 | ex.printStackTrace(); |
| 180 | } |
Richard S. Hall | 930fecc | 2005-08-16 18:33:34 +0000 | [diff] [blame] | 181 | } |
| 182 | } |
| 183 | } |
Richard S. Hall | cd48fc6 | 2009-05-18 15:22:37 +0000 | [diff] [blame] | 184 | catch (IOException ex) |
| 185 | { |
| 186 | // Any IO error causes the thread to exit. |
| 187 | System.err.println("ShellTUI: Unable to read from stdin...exiting."); |
| 188 | } |
Richard S. Hall | 930fecc | 2005-08-16 18:33:34 +0000 | [diff] [blame] | 189 | } |
| 190 | } |
Richard S. Hall | cd48fc6 | 2009-05-18 15:22:37 +0000 | [diff] [blame] | 191 | } |