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