blob: caa3e049d7e09604d570cbfe8357b20d0f0b5293 [file] [log] [blame]
Richard S. Hallfcaa2d42006-09-28 19:56:03 +00001/*
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. Hall930fecc2005-08-16 18:33:34 +00009 *
Richard S. Hallfcaa2d42006-09-28 19:56:03 +000010 * http://www.apache.org/licenses/LICENSE-2.0
Richard S. Hall930fecc2005-08-16 18:33:34 +000011 *
Richard S. Hallfcaa2d42006-09-28 19:56:03 +000012 * 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. Hall930fecc2005-08-16 18:33:34 +000018 */
Richard S. Hall71644942006-02-08 14:29:10 +000019package org.apache.felix.shell.tui;
Richard S. Hall930fecc2005-08-16 18:33:34 +000020
21import java.io.*;
22
Richard S. Hall5a031592005-08-19 19:53:58 +000023import org.apache.felix.shell.ShellService;
Richard S. Hall930fecc2005-08-16 18:33:34 +000024import org.osgi.framework.*;
25
26public class Activator implements BundleActivator
27{
Richard S. Hall66b5a562009-09-06 19:09:57 +000028 private static final String CHECK_INPUT_PROP = "shell.tui.checkinput";
29
Richard S. Hall59815312006-03-17 15:57:03 +000030 private BundleContext m_context = null;
Richard S. Hall9203b4c2008-09-23 14:03:15 +000031 private volatile ShellTuiRunnable m_runnable = null;
32 private volatile Thread m_thread = null;
Richard S. Hall59815312006-03-17 15:57:03 +000033 private ServiceReference m_shellRef = null;
34 private ShellService m_shell = null;
Richard S. Hall66b5a562009-09-06 19:09:57 +000035 private volatile boolean m_checkInput = false;
Richard S. Hall930fecc2005-08-16 18:33:34 +000036
37 public void start(BundleContext context)
38 {
39 m_context = context;
40
Richard S. Hall66b5a562009-09-06 19:09:57 +000041 // 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. Hall5a031592005-08-19 19:53:58 +000045 // Listen for registering/unregistering impl service.
Richard S. Hall930fecc2005-08-16 18:33:34 +000046 ServiceListener sl = new ServiceListener() {
47 public void serviceChanged(ServiceEvent event)
48 {
49 synchronized (Activator.this)
50 {
Richard S. Hall930fecc2005-08-16 18:33:34 +000051 // Initialize the service if we don't have one.
Richard S. Hallcd48fc62009-05-18 15:22:37 +000052 if ((event.getType() == ServiceEvent.REGISTERED)
Richard S. Hall930fecc2005-08-16 18:33:34 +000053 && (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. Hall59815312006-03-17 15:57:03 +000070 try
71 {
Richard S. Hall930fecc2005-08-16 18:33:34 +000072 m_context.addServiceListener(sl,
73 "(objectClass="
Richard S. Hall5a031592005-08-19 19:53:58 +000074 + org.apache.felix.shell.ShellService.class.getName()
Richard S. Hall930fecc2005-08-16 18:33:34 +000075 + ")");
Richard S. Hall59815312006-03-17 15:57:03 +000076 }
77 catch (InvalidSyntaxException ex)
78 {
79 System.err.println("ShellTui: Cannot add service listener.");
80 System.err.println("ShellTui: " + ex);
Richard S. Hall930fecc2005-08-16 18:33:34 +000081 }
82
Richard S. Hall5a031592005-08-19 19:53:58 +000083 // Now try to manually initialize the impl service
Richard S. Hall930fecc2005-08-16 18:33:34 +000084 // since one might already be available.
85 initializeService();
86
Karl Pauls957ca192008-04-21 19:55:56 +000087 // Start impl thread.
88 m_thread = new Thread(
89 m_runnable = new ShellTuiRunnable(),
90 "Felix Shell TUI");
Richard S. Hall59815312006-03-17 15:57:03 +000091 m_thread.start();
Richard S. Hall930fecc2005-08-16 18:33:34 +000092 }
93
94 private synchronized void initializeService()
95 {
Richard S. Hallcd48fc62009-05-18 15:22:37 +000096 if (m_shell == null)
Richard S. Hall59815312006-03-17 15:57:03 +000097 {
Richard S. Hallcd48fc62009-05-18 15:22:37 +000098 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. Hall59815312006-03-17 15:57:03 +0000104 }
Richard S. Hall930fecc2005-08-16 18:33:34 +0000105 }
106
107 public void stop(BundleContext context)
108 {
Karl Pauls957ca192008-04-21 19:55:56 +0000109 if (m_runnable != null)
Richard S. Hall930fecc2005-08-16 18:33:34 +0000110 {
Karl Pauls957ca192008-04-21 19:55:56 +0000111 m_runnable.stop();
Richard S. Hall930fecc2005-08-16 18:33:34 +0000112 }
113 }
114
115 private class ShellTuiRunnable implements Runnable
116 {
Richard S. Hall32802512008-11-21 22:10:58 +0000117 private volatile boolean m_stop = false;
Richard S. Hall734d7122008-11-21 21:56:55 +0000118
Richard S. Hall930fecc2005-08-16 18:33:34 +0000119 public void stop()
120 {
Richard S. Hall32802512008-11-21 22:10:58 +0000121 m_stop = true;
Richard S. Hall930fecc2005-08-16 18:33:34 +0000122 }
123
124 public void run()
125 {
126 String line = null;
127 BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
Karl Pauls957ca192008-04-21 19:55:56 +0000128
Richard S. Hall32802512008-11-21 22:10:58 +0000129 try
130 {
Richard S. Hallcd48fc62009-05-18 15:22:37 +0000131 boolean needPrompt = true;
132 int available;
133 while (!m_stop)
Richard S. Hall59815312006-03-17 15:57:03 +0000134 {
Richard S. Hallcd48fc62009-05-18 15:22:37 +0000135 if (needPrompt)
136 {
137 System.out.print("-> ");
138 needPrompt = false;
139 }
140
Richard S. Hall66b5a562009-09-06 19:09:57 +0000141 if (m_checkInput)
Richard S. Hallcd48fc62009-05-18 15:22:37 +0000142 {
Richard S. Hall66b5a562009-09-06 19:09:57 +0000143 available = System.in.available();
144
145 if (available == 0)
Richard S. Hallcd48fc62009-05-18 15:22:37 +0000146 {
Richard S. Hall66b5a562009-09-06 19:09:57 +0000147 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. Hallcd48fc62009-05-18 15:22:37 +0000157 }
Richard S. Hallcd48fc62009-05-18 15:22:37 +0000158 }
159
Richard S. Hall930fecc2005-08-16 18:33:34 +0000160 line = in.readLine();
Richard S. Hall9203b4c2008-09-23 14:03:15 +0000161 if (line == null)
162 {
163 System.err.println("ShellTUI: No standard input...exiting.");
164 break;
165 }
Richard S. Hallcd48fc62009-05-18 15:22:37 +0000166 needPrompt = true;
Richard S. Hall930fecc2005-08-16 18:33:34 +0000167
Richard S. Hall930fecc2005-08-16 18:33:34 +0000168 line = line.trim();
Richard S. Hall930fecc2005-08-16 18:33:34 +0000169 if (line.length() == 0)
170 {
171 continue;
172 }
173
Richard S. Hallcd48fc62009-05-18 15:22:37 +0000174 synchronized (Activator.this)
Richard S. Hall59815312006-03-17 15:57:03 +0000175 {
Richard S. Hallcd48fc62009-05-18 15:22:37 +0000176 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. Hall930fecc2005-08-16 18:33:34 +0000191 }
192 }
193 }
Richard S. Hallcd48fc62009-05-18 15:22:37 +0000194 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. Hall930fecc2005-08-16 18:33:34 +0000199 }
200 }
Richard S. Hallcd48fc62009-05-18 15:22:37 +0000201}