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 | { |
| 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. Hall | 5981531 | 2006-03-17 15:57:03 +0000 | [diff] [blame] | 69 | try |
| 70 | { |
Richard S. Hall | 930fecc | 2005-08-16 18:33:34 +0000 | [diff] [blame] | 71 | m_context.addServiceListener(sl, |
| 72 | "(objectClass=" |
Richard S. Hall | 5a03159 | 2005-08-19 19:53:58 +0000 | [diff] [blame] | 73 | + org.apache.felix.shell.ShellService.class.getName() |
Richard S. Hall | 930fecc | 2005-08-16 18:33:34 +0000 | [diff] [blame] | 74 | + ")"); |
Richard S. Hall | 5981531 | 2006-03-17 15:57:03 +0000 | [diff] [blame] | 75 | } |
| 76 | catch (InvalidSyntaxException ex) |
| 77 | { |
| 78 | System.err.println("ShellTui: Cannot add service listener."); |
| 79 | System.err.println("ShellTui: " + ex); |
Richard S. Hall | 930fecc | 2005-08-16 18:33:34 +0000 | [diff] [blame] | 80 | } |
| 81 | |
Richard S. Hall | 5a03159 | 2005-08-19 19:53:58 +0000 | [diff] [blame] | 82 | // Now try to manually initialize the impl service |
Richard S. Hall | 930fecc | 2005-08-16 18:33:34 +0000 | [diff] [blame] | 83 | // since one might already be available. |
| 84 | initializeService(); |
| 85 | |
Karl Pauls | 957ca19 | 2008-04-21 19:55:56 +0000 | [diff] [blame] | 86 | // Start impl thread. |
| 87 | m_thread = new Thread( |
| 88 | m_runnable = new ShellTuiRunnable(), |
| 89 | "Felix Shell TUI"); |
Richard S. Hall | 5981531 | 2006-03-17 15:57:03 +0000 | [diff] [blame] | 90 | m_thread.start(); |
Richard S. Hall | 930fecc | 2005-08-16 18:33:34 +0000 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | private synchronized void initializeService() |
| 94 | { |
| 95 | if (m_shell != null) |
Richard S. Hall | 5981531 | 2006-03-17 15:57:03 +0000 | [diff] [blame] | 96 | { |
Richard S. Hall | 930fecc | 2005-08-16 18:33:34 +0000 | [diff] [blame] | 97 | return; |
Richard S. Hall | 5981531 | 2006-03-17 15:57:03 +0000 | [diff] [blame] | 98 | } |
Richard S. Hall | 930fecc | 2005-08-16 18:33:34 +0000 | [diff] [blame] | 99 | m_shellRef = m_context.getServiceReference( |
Richard S. Hall | 5a03159 | 2005-08-19 19:53:58 +0000 | [diff] [blame] | 100 | org.apache.felix.shell.ShellService.class.getName()); |
Richard S. Hall | 930fecc | 2005-08-16 18:33:34 +0000 | [diff] [blame] | 101 | if (m_shellRef == null) |
Richard S. Hall | 5981531 | 2006-03-17 15:57:03 +0000 | [diff] [blame] | 102 | { |
Richard S. Hall | 930fecc | 2005-08-16 18:33:34 +0000 | [diff] [blame] | 103 | return; |
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 | m_shell = (ShellService) m_context.getService(m_shellRef); |
| 106 | } |
| 107 | |
| 108 | public void stop(BundleContext context) |
| 109 | { |
Karl Pauls | 957ca19 | 2008-04-21 19:55:56 +0000 | [diff] [blame] | 110 | if (m_runnable != null) |
Richard S. Hall | 930fecc | 2005-08-16 18:33:34 +0000 | [diff] [blame] | 111 | { |
Karl Pauls | 957ca19 | 2008-04-21 19:55:56 +0000 | [diff] [blame] | 112 | m_runnable.stop(); |
| 113 | m_thread.interrupt(); |
Richard S. Hall | 930fecc | 2005-08-16 18:33:34 +0000 | [diff] [blame] | 114 | } |
| 115 | } |
| 116 | |
| 117 | private class ShellTuiRunnable implements Runnable |
| 118 | { |
Richard S. Hall | 9203b4c | 2008-09-23 14:03:15 +0000 | [diff] [blame] | 119 | private volatile boolean stop = false; |
Richard S. Hall | 930fecc | 2005-08-16 18:33:34 +0000 | [diff] [blame] | 120 | |
| 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 Pauls | 957ca19 | 2008-04-21 19:55:56 +0000 | [diff] [blame] | 130 | |
Richard S. Hall | 930fecc | 2005-08-16 18:33:34 +0000 | [diff] [blame] | 131 | while (!stop) |
| 132 | { |
| 133 | System.out.print("-> "); |
| 134 | |
Richard S. Hall | 5981531 | 2006-03-17 15:57:03 +0000 | [diff] [blame] | 135 | try |
| 136 | { |
Richard S. Hall | 930fecc | 2005-08-16 18:33:34 +0000 | [diff] [blame] | 137 | line = in.readLine(); |
Richard S. Hall | 5981531 | 2006-03-17 15:57:03 +0000 | [diff] [blame] | 138 | } |
| 139 | catch (IOException ex) |
| 140 | { |
Richard S. Hall | 930fecc | 2005-08-16 18:33:34 +0000 | [diff] [blame] | 141 | System.err.println("Could not read input, please try again."); |
Richard S. Hall | 8e39b69 | 2008-09-25 22:33:36 +0000 | [diff] [blame^] | 142 | break; |
Richard S. Hall | 930fecc | 2005-08-16 18:33:34 +0000 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | synchronized (Activator.this) |
| 146 | { |
Richard S. Hall | 9203b4c | 2008-09-23 14:03:15 +0000 | [diff] [blame] | 147 | if (line == null) |
| 148 | { |
| 149 | System.err.println("ShellTUI: No standard input...exiting."); |
| 150 | break; |
| 151 | } |
| 152 | |
Richard S. Hall | 930fecc | 2005-08-16 18:33:34 +0000 | [diff] [blame] | 153 | if (m_shell == null) |
| 154 | { |
Richard S. Hall | 5a03159 | 2005-08-19 19:53:58 +0000 | [diff] [blame] | 155 | System.out.println("No impl service available."); |
Richard S. Hall | 930fecc | 2005-08-16 18:33:34 +0000 | [diff] [blame] | 156 | continue; |
| 157 | } |
| 158 | |
Richard S. Hall | 930fecc | 2005-08-16 18:33:34 +0000 | [diff] [blame] | 159 | line = line.trim(); |
| 160 | |
| 161 | if (line.length() == 0) |
| 162 | { |
| 163 | continue; |
| 164 | } |
| 165 | |
Richard S. Hall | 5981531 | 2006-03-17 15:57:03 +0000 | [diff] [blame] | 166 | try |
| 167 | { |
Richard S. Hall | 930fecc | 2005-08-16 18:33:34 +0000 | [diff] [blame] | 168 | m_shell.executeCommand(line, System.out, System.err); |
Richard S. Hall | 5981531 | 2006-03-17 15:57:03 +0000 | [diff] [blame] | 169 | } |
| 170 | catch (Exception ex) |
| 171 | { |
Richard S. Hall | 9203b4c | 2008-09-23 14:03:15 +0000 | [diff] [blame] | 172 | System.err.println("ShellTUI: " + ex); |
Richard S. Hall | 930fecc | 2005-08-16 18:33:34 +0000 | [diff] [blame] | 173 | ex.printStackTrace(); |
| 174 | } |
| 175 | } |
| 176 | } |
| 177 | } |
| 178 | } |
Karl Pauls | 957ca19 | 2008-04-21 19:55:56 +0000 | [diff] [blame] | 179 | } |