blob: 993482a11dab20026f45cffee721da4590e54884 [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. Hall59815312006-03-17 15:57:03 +000028 private BundleContext m_context = null;
Richard S. Hall9203b4c2008-09-23 14:03:15 +000029 private volatile ShellTuiRunnable m_runnable = null;
30 private volatile Thread m_thread = null;
Richard S. Hall59815312006-03-17 15:57:03 +000031 private ServiceReference m_shellRef = null;
32 private ShellService m_shell = null;
Richard S. Hall930fecc2005-08-16 18:33:34 +000033
34 public void start(BundleContext context)
35 {
36 m_context = context;
37
Richard S. Hall5a031592005-08-19 19:53:58 +000038 // Listen for registering/unregistering impl service.
Richard S. Hall930fecc2005-08-16 18:33:34 +000039 ServiceListener sl = new ServiceListener() {
40 public void serviceChanged(ServiceEvent event)
41 {
42 synchronized (Activator.this)
43 {
Richard S. Hall930fecc2005-08-16 18:33:34 +000044 // Initialize the service if we don't have one.
Richard S. Hallcd48fc62009-05-18 15:22:37 +000045 if ((event.getType() == ServiceEvent.REGISTERED)
Richard S. Hall930fecc2005-08-16 18:33:34 +000046 && (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. Hall59815312006-03-17 15:57:03 +000063 try
64 {
Richard S. Hall930fecc2005-08-16 18:33:34 +000065 m_context.addServiceListener(sl,
66 "(objectClass="
Richard S. Hall5a031592005-08-19 19:53:58 +000067 + org.apache.felix.shell.ShellService.class.getName()
Richard S. Hall930fecc2005-08-16 18:33:34 +000068 + ")");
Richard S. Hall59815312006-03-17 15:57:03 +000069 }
70 catch (InvalidSyntaxException ex)
71 {
72 System.err.println("ShellTui: Cannot add service listener.");
73 System.err.println("ShellTui: " + ex);
Richard S. Hall930fecc2005-08-16 18:33:34 +000074 }
75
Richard S. Hall5a031592005-08-19 19:53:58 +000076 // Now try to manually initialize the impl service
Richard S. Hall930fecc2005-08-16 18:33:34 +000077 // since one might already be available.
78 initializeService();
79
Karl Pauls957ca192008-04-21 19:55:56 +000080 // Start impl thread.
81 m_thread = new Thread(
82 m_runnable = new ShellTuiRunnable(),
83 "Felix Shell TUI");
Richard S. Hall59815312006-03-17 15:57:03 +000084 m_thread.start();
Richard S. Hall930fecc2005-08-16 18:33:34 +000085 }
86
87 private synchronized void initializeService()
88 {
Richard S. Hallcd48fc62009-05-18 15:22:37 +000089 if (m_shell == null)
Richard S. Hall59815312006-03-17 15:57:03 +000090 {
Richard S. Hallcd48fc62009-05-18 15:22:37 +000091 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. Hall59815312006-03-17 15:57:03 +000097 }
Richard S. Hall930fecc2005-08-16 18:33:34 +000098 }
99
100 public void stop(BundleContext context)
101 {
Karl Pauls957ca192008-04-21 19:55:56 +0000102 if (m_runnable != null)
Richard S. Hall930fecc2005-08-16 18:33:34 +0000103 {
Karl Pauls957ca192008-04-21 19:55:56 +0000104 m_runnable.stop();
Richard S. Hall930fecc2005-08-16 18:33:34 +0000105 }
106 }
107
108 private class ShellTuiRunnable implements Runnable
109 {
Richard S. Hall32802512008-11-21 22:10:58 +0000110 private volatile boolean m_stop = false;
Richard S. Hall734d7122008-11-21 21:56:55 +0000111
Richard S. Hall930fecc2005-08-16 18:33:34 +0000112 public void stop()
113 {
Richard S. Hall32802512008-11-21 22:10:58 +0000114 m_stop = true;
Richard S. Hall930fecc2005-08-16 18:33:34 +0000115 }
116
117 public void run()
118 {
119 String line = null;
120 BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
Karl Pauls957ca192008-04-21 19:55:56 +0000121
Richard S. Hall32802512008-11-21 22:10:58 +0000122 try
123 {
Richard S. Hallcd48fc62009-05-18 15:22:37 +0000124 boolean needPrompt = true;
125 int available;
126 while (!m_stop)
Richard S. Hall59815312006-03-17 15:57:03 +0000127 {
Richard S. Hallcd48fc62009-05-18 15:22:37 +0000128 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. Hall930fecc2005-08-16 18:33:34 +0000150 line = in.readLine();
Richard S. Hall9203b4c2008-09-23 14:03:15 +0000151 if (line == null)
152 {
153 System.err.println("ShellTUI: No standard input...exiting.");
154 break;
155 }
Richard S. Hallcd48fc62009-05-18 15:22:37 +0000156 needPrompt = true;
Richard S. Hall930fecc2005-08-16 18:33:34 +0000157
Richard S. Hall930fecc2005-08-16 18:33:34 +0000158 line = line.trim();
Richard S. Hall930fecc2005-08-16 18:33:34 +0000159 if (line.length() == 0)
160 {
161 continue;
162 }
163
Richard S. Hallcd48fc62009-05-18 15:22:37 +0000164 synchronized (Activator.this)
Richard S. Hall59815312006-03-17 15:57:03 +0000165 {
Richard S. Hallcd48fc62009-05-18 15:22:37 +0000166 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. Hall930fecc2005-08-16 18:33:34 +0000181 }
182 }
183 }
Richard S. Hallcd48fc62009-05-18 15:22:37 +0000184 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. Hall930fecc2005-08-16 18:33:34 +0000189 }
190 }
Richard S. Hallcd48fc62009-05-18 15:22:37 +0000191}