blob: d356f150f5cfaa452120516a29f7c76ca0f98e16 [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 {
44 // Ignore additional services if we already have one.
45 if ((event.getType() == ServiceEvent.REGISTERED)
46 && (m_shellRef != null))
47 {
48 return;
49 }
50 // Initialize the service if we don't have one.
51 else if ((event.getType() == ServiceEvent.REGISTERED)
52 && (m_shellRef == null))
53 {
54 initializeService();
55 }
56 // Unget the service if it is unregistering.
57 else if ((event.getType() == ServiceEvent.UNREGISTERING)
58 && event.getServiceReference().equals(m_shellRef))
59 {
60 m_context.ungetService(m_shellRef);
61 m_shellRef = null;
62 m_shell = null;
63 // Try to get another service.
64 initializeService();
65 }
66 }
67 }
68 };
Richard S. Hall59815312006-03-17 15:57:03 +000069 try
70 {
Richard S. Hall930fecc2005-08-16 18:33:34 +000071 m_context.addServiceListener(sl,
72 "(objectClass="
Richard S. Hall5a031592005-08-19 19:53:58 +000073 + org.apache.felix.shell.ShellService.class.getName()
Richard S. Hall930fecc2005-08-16 18:33:34 +000074 + ")");
Richard S. Hall59815312006-03-17 15:57:03 +000075 }
76 catch (InvalidSyntaxException ex)
77 {
78 System.err.println("ShellTui: Cannot add service listener.");
79 System.err.println("ShellTui: " + ex);
Richard S. Hall930fecc2005-08-16 18:33:34 +000080 }
81
Richard S. Hall5a031592005-08-19 19:53:58 +000082 // Now try to manually initialize the impl service
Richard S. Hall930fecc2005-08-16 18:33:34 +000083 // since one might already be available.
84 initializeService();
85
Karl Pauls957ca192008-04-21 19:55:56 +000086 // Start impl thread.
87 m_thread = new Thread(
88 m_runnable = new ShellTuiRunnable(),
89 "Felix Shell TUI");
Richard S. Hall59815312006-03-17 15:57:03 +000090 m_thread.start();
Richard S. Hall930fecc2005-08-16 18:33:34 +000091 }
92
93 private synchronized void initializeService()
94 {
95 if (m_shell != null)
Richard S. Hall59815312006-03-17 15:57:03 +000096 {
Richard S. Hall930fecc2005-08-16 18:33:34 +000097 return;
Richard S. Hall59815312006-03-17 15:57:03 +000098 }
Richard S. Hall930fecc2005-08-16 18:33:34 +000099 m_shellRef = m_context.getServiceReference(
Richard S. Hall5a031592005-08-19 19:53:58 +0000100 org.apache.felix.shell.ShellService.class.getName());
Richard S. Hall930fecc2005-08-16 18:33:34 +0000101 if (m_shellRef == null)
Richard S. Hall59815312006-03-17 15:57:03 +0000102 {
Richard S. Hall930fecc2005-08-16 18:33:34 +0000103 return;
Richard S. Hall59815312006-03-17 15:57:03 +0000104 }
Richard S. Hall930fecc2005-08-16 18:33:34 +0000105 m_shell = (ShellService) m_context.getService(m_shellRef);
106 }
107
108 public void stop(BundleContext context)
109 {
Karl Pauls957ca192008-04-21 19:55:56 +0000110 if (m_runnable != null)
Richard S. Hall930fecc2005-08-16 18:33:34 +0000111 {
Karl Pauls957ca192008-04-21 19:55:56 +0000112 m_runnable.stop();
113 m_thread.interrupt();
Richard S. Hall930fecc2005-08-16 18:33:34 +0000114 }
115 }
116
117 private class ShellTuiRunnable implements Runnable
118 {
Richard S. Hall9203b4c2008-09-23 14:03:15 +0000119 private volatile boolean stop = false;
Richard S. Hall930fecc2005-08-16 18:33:34 +0000120
121 public void stop()
122 {
123 stop = true;
124 }
125
126 public void run()
127 {
128 String line = null;
129 BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
Karl Pauls957ca192008-04-21 19:55:56 +0000130
Richard S. Hall930fecc2005-08-16 18:33:34 +0000131 while (!stop)
132 {
133 System.out.print("-> ");
134
Richard S. Hall59815312006-03-17 15:57:03 +0000135 try
136 {
Richard S. Hall930fecc2005-08-16 18:33:34 +0000137 line = in.readLine();
Richard S. Hall59815312006-03-17 15:57:03 +0000138 }
139 catch (IOException ex)
140 {
Richard S. Hall930fecc2005-08-16 18:33:34 +0000141 System.err.println("Could not read input, please try again.");
Richard S. Hall8e39b692008-09-25 22:33:36 +0000142 break;
Richard S. Hall930fecc2005-08-16 18:33:34 +0000143 }
144
145 synchronized (Activator.this)
146 {
Richard S. Hall9203b4c2008-09-23 14:03:15 +0000147 if (line == null)
148 {
149 System.err.println("ShellTUI: No standard input...exiting.");
150 break;
151 }
152
Richard S. Hall930fecc2005-08-16 18:33:34 +0000153 if (m_shell == null)
154 {
Richard S. Hall5a031592005-08-19 19:53:58 +0000155 System.out.println("No impl service available.");
Richard S. Hall930fecc2005-08-16 18:33:34 +0000156 continue;
157 }
158
Richard S. Hall930fecc2005-08-16 18:33:34 +0000159 line = line.trim();
160
161 if (line.length() == 0)
162 {
163 continue;
164 }
165
Richard S. Hall59815312006-03-17 15:57:03 +0000166 try
167 {
Richard S. Hall930fecc2005-08-16 18:33:34 +0000168 m_shell.executeCommand(line, System.out, System.err);
Richard S. Hall59815312006-03-17 15:57:03 +0000169 }
170 catch (Exception ex)
171 {
Richard S. Hall9203b4c2008-09-23 14:03:15 +0000172 System.err.println("ShellTUI: " + ex);
Richard S. Hall930fecc2005-08-16 18:33:34 +0000173 ex.printStackTrace();
174 }
175 }
176 }
177 }
178 }
Karl Pauls957ca192008-04-21 19:55:56 +0000179}