Richard S. Hall | 677f589 | 2007-07-10 15:29:11 +0000 | [diff] [blame] | 1 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> |
Karl Pauls | 6450827 | 2009-09-06 21:15:38 +0000 | [diff] [blame^] | 2 | <html><head> |
Richard S. Hall | 677f589 | 2007-07-10 15:29:11 +0000 | [diff] [blame] | 3 | |
| 4 | |
| 5 | |
Karl Pauls | 6450827 | 2009-09-06 21:15:38 +0000 | [diff] [blame^] | 6 | <title>Apache Felix - Apache Felix Shell Service</title> |
Richard S. Hall | 677f589 | 2007-07-10 15:29:11 +0000 | [diff] [blame] | 7 | <link rel="stylesheet" href="apache-felix-shell-service_files/site.css" type="text/css" media="all"> |
Karl Pauls | 6450827 | 2009-09-06 21:15:38 +0000 | [diff] [blame^] | 8 | <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> |
| 9 | </head><body> |
| 10 | <div class="title"><div class="logo"><a href="http://felix.apache.org/site/index.html"><img alt="Apache Felix" src="apache-felix-shell-service_files/logo.png" border="0"></a></div><div class="header"><a href="http://www.apache.org/"><img alt="Apache" src="apache-felix-shell-service-Dateien/apache.png" border="0"></a></div></div> |
Richard S. Hall | 677f589 | 2007-07-10 15:29:11 +0000 | [diff] [blame] | 11 | <div class="menu"> |
Karl Pauls | b26e3f4 | 2008-04-21 22:17:48 +0000 | [diff] [blame] | 12 | <ul> |
| 13 | <li><a href="http://felix.apache.org/site/news.html" title="news">news</a></li> |
| 14 | <li><a href="http://felix.apache.org/site/license.html" title="license">license</a></li> |
Karl Pauls | 6450827 | 2009-09-06 21:15:38 +0000 | [diff] [blame^] | 15 | <li><a href="http://felix.apache.org/site/downloads.cgi" rel="nofollow">downloads</a></li> |
Karl Pauls | b26e3f4 | 2008-04-21 22:17:48 +0000 | [diff] [blame] | 16 | <li><a href="http://felix.apache.org/site/documentation.html" title="documentation">documentation</a></li> |
| 17 | <li><a href="http://felix.apache.org/site/mailinglists.html" title="mailinglists">mailing lists</a></li> |
| 18 | <li><a href="http://felix.apache.org/site/contributing.html" title="Contributing">contributing</a></li> |
Karl Pauls | 6450827 | 2009-09-06 21:15:38 +0000 | [diff] [blame^] | 19 | <li><a href="http://www.apache.org/" rel="nofollow">asf</a></li> |
| 20 | <li><a href="http://www.apache.org/foundation/sponsorship.html" rel="nofollow">sponsorship</a></li> |
| 21 | <li><a href="http://www.apache.org/foundation/thanks.html" rel="nofollow">sponsors</a> |
Karl Pauls | b26e3f4 | 2008-04-21 22:17:48 +0000 | [diff] [blame] | 22 | <!-- ApacheCon Ad --> |
| 23 | <iframe src="apache-felix-shell-service_files/button.html" style="border-width: 0pt; float: left;" frameborder="0" height="135" scrolling="no" width="135"></iframe> |
| 24 | <p style="height: 100px;"> |
| 25 | <!-- ApacheCon Ad --> |
| 26 | </p></li></ul> </div> |
Richard S. Hall | 677f589 | 2007-07-10 15:29:11 +0000 | [diff] [blame] | 27 | <div class="main"> |
| 28 | <h1><a name="ApacheFelixShellService-ApacheFelixShellService"></a>Apache Felix Shell Service</h1> |
| 29 | |
| 30 | <ul> |
Karl Pauls | 6450827 | 2009-09-06 21:15:38 +0000 | [diff] [blame^] | 31 | <li><a href="#ApacheFelixShellService-overview">Overview</a></li> |
| 32 | <li><a href="#ApacheFelixShellService-service">How the Shell Service Works</a></li> |
| 33 | <li><a href="#ApacheFelixShellService-commands">How Commands Work</a></li> |
| 34 | <li><a href="#ApacheFelixShellService-creating">Creating a Command</a></li> |
| 35 | <li><a href="#ApacheFelixShellService-security">Security and the Shell Service</a></li> |
| 36 | <li><a href="#ApacheFelixShellService-feedback">Feedback</a></li> |
Richard S. Hall | 677f589 | 2007-07-10 15:29:11 +0000 | [diff] [blame] | 37 | </ul> |
| 38 | |
| 39 | |
| 40 | <p><a name="ApacheFelixShellService-overview"></a></p> |
| 41 | |
| 42 | <h2><a name="ApacheFelixShellService-Overview"></a>Overview</h2> |
| 43 | |
| 44 | <p>In order to interact with Felix it is necessary to have some sort of |
| 45 | interactive shell that allows you to issue commands to the framework |
| 46 | and to obtain information from it. The OSGi specification does not |
| 47 | define how an OSGi framework should provide this interactivity. Felix |
| 48 | defines a shell service for creating and executing arbitrary commands. |
| 49 | The shell service does not define a user interface, only a service API.</p> |
| 50 | |
| 51 | <p>The benefit of the Felix shell service approach is that it is possible to:</p> |
| 52 | |
| 53 | <ul> |
| 54 | <li>have multiple shell user interfaces (e.g., textual and graphical),</li> |
| 55 | <li>add custom commands to the shell (i.e., bundles can make commands available via the shell service), and</li> |
| 56 | <li>use the shell service from other bundles/services.</li> |
| 57 | </ul> |
| 58 | |
| 59 | |
| 60 | <p>The remainder of this document describes how the shell service works |
| 61 | and how to create custom commands for it. This document does not |
| 62 | describe how to use the command shell, nor does it describe the |
| 63 | text-based or GUI-based user interfaces that are available for the |
| 64 | shell.</p> |
| 65 | |
| 66 | <p><a name="ApacheFelixShellService-service"></a></p> |
| 67 | |
| 68 | <h2><a name="ApacheFelixShellService-HowtheShellServiceWorks"></a>How the Shell Service Works</h2> |
| 69 | |
| 70 | <p>The Felix shell service is intended to be a simple, but extensible |
| 71 | shell service that can have multiple user interface implementations, |
| 72 | all of which are independent from the Felix framework. The shell |
| 73 | service is currently not intended to be sophisticated, rather it is |
| 74 | just a mechanism to execute commands. The shell service maintains a |
| 75 | list of command services, each of which have a unique command name. The |
| 76 | shell service is defined by the following service interface:</p> |
| 77 | |
Karl Pauls | 6450827 | 2009-09-06 21:15:38 +0000 | [diff] [blame^] | 78 | <style type="text/css"> |
| 79 | @import url(/confluence/download/resources/confluence.ext.code:code/shStyles.css); |
| 80 | </style> |
| 81 | <!--[if IE]> |
| 82 | <style type="text/css"> |
| 83 | .code textarea, .code input { padding: 0 !important; } |
| 84 | </style> |
| 85 | <![endif]--> |
| 86 | <script class="javascript" src="apache-felix-shell-service_files/shCore.js"></script> |
| 87 | <script class="javascript" src="apache-felix-shell-service_files/shBrushCSharp.js"></script> |
| 88 | <script class="javascript" src="apache-felix-shell-service_files/shBrushPhp.js"></script> |
| 89 | <script class="javascript" src="apache-felix-shell-service_files/shBrushJScript.js"></script> |
| 90 | <script class="javascript" src="apache-felix-shell-service_files/shBrushVb.js"></script> |
| 91 | <script class="javascript" src="apache-felix-shell-service_files/shBrushSql.js"></script> |
| 92 | <script class="javascript" src="apache-felix-shell-service_files/shBrushXml.js"></script> |
| 93 | <script class="javascript" src="apache-felix-shell-service_files/shBrushShell.html"></script> |
| 94 | <script class="javascript" src="apache-felix-shell-service_files/shBrushDelphi.js"></script> |
| 95 | <script class="javascript" src="apache-felix-shell-service_files/shBrushPython.js"></script> |
| 96 | <script class="javascript" src="apache-felix-shell-service_files/shBrushJava.js"></script> |
| 97 | <div class="code"> |
| 98 | <textarea name="newcodemacro" class="java:nocontrols:nogutter" rows="10" readonly="readonly">package org.apache.felix.shell; |
Richard S. Hall | 677f589 | 2007-07-10 15:29:11 +0000 | [diff] [blame] | 99 | |
Karl Pauls | 6450827 | 2009-09-06 21:15:38 +0000 | [diff] [blame^] | 100 | public interface ShellService |
Richard S. Hall | 677f589 | 2007-07-10 15:29:11 +0000 | [diff] [blame] | 101 | { |
Karl Pauls | 6450827 | 2009-09-06 21:15:38 +0000 | [diff] [blame^] | 102 | public String[] getCommands(); |
| 103 | public String getCommandUsage(String name); |
| 104 | public String getCommandDescription(String name); |
| 105 | public ServiceReference getCommandReference(String name); |
| 106 | public void executeCommand( |
| 107 | String commandLine, PrintStream out, PrintStream err) |
| 108 | throws Exception; |
| 109 | }</textarea> |
| 110 | <script class="javascript"> |
| 111 | if(!window.newcodemacro_initialised) |
| 112 | { |
| 113 | window.newcodemacro_initialised = true; |
| 114 | window.oldonloadmethod = window.onload; |
| 115 | window.onload = function(){ |
| 116 | dp.SyntaxHighlighter.HighlightAll('newcodemacro'); |
| 117 | if(window.oldonloadmethod) |
| 118 | { |
| 119 | window.oldonloadmethod(); |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | </script> |
| 125 | </div> |
| 126 | |
Richard S. Hall | 677f589 | 2007-07-10 15:29:11 +0000 | [diff] [blame] | 127 | |
| 128 | <p>Using the shell service interface, it is possible to access and |
| 129 | execute available commands. The shell service methods perform the |
| 130 | following functions:</p> |
| 131 | |
| 132 | <ul> |
| 133 | <li><tt>getCommands()</tt> - returns an array of strings that correspond to the names of the installed shell commands.</li> |
| 134 | <li><tt>getCommandUsage()</tt> - returns the command usage string for a particular command name</li> |
| 135 | <li><tt>getCommandDescription()</tt> - returns a short description for a particular command name.</li> |
| 136 | <li><tt>getCommandReference()</tt> - returns the service reference for a particular command name.</li> |
| 137 | <li><tt>executeCommand()</tt> - executes a particular command using the specified command line and print streams.</li> |
| 138 | </ul> |
| 139 | |
| 140 | |
| 141 | <p>Most of the shell service methods require no explanation except for |
| 142 | the executeCommand() method. Even though this method is the most |
| 143 | complex, it is still fairly simplistic. The assumption of the shell |
| 144 | service is that a command line will be typed by the user (or perhaps |
| 145 | constructed by a GUI) and passed into it for execution. The shell |
| 146 | service interprets the command line in a very simplistic fashion; it |
| 147 | takes the leading string of characters terminated by a space character |
| 148 | (not including it) and assumes that this leading token is the command |
| 149 | name. Consider the following command line:</p> |
| 150 | |
Karl Pauls | 6450827 | 2009-09-06 21:15:38 +0000 | [diff] [blame^] | 151 | <div class="preformatted panel" style="border-width: 1px;"><div class="preformattedContent panelContent"> |
Karl Pauls | b26e3f4 | 2008-04-21 22:17:48 +0000 | [diff] [blame] | 152 | <pre>update 3 http://www.foo.com/bar.jar |
Richard S. Hall | 677f589 | 2007-07-10 15:29:11 +0000 | [diff] [blame] | 153 | </pre> |
| 154 | </div></div> |
| 155 | |
| 156 | <p>The shell service interprets this as an <tt>update</tt> command and |
| 157 | will search for a command service with the same name. If a |
| 158 | corresponding command service is not found, then it will print an error |
| 159 | message to the error print stream. If a corresponding command service |
| 160 | is found, then it will pass the entire command line string and the |
| 161 | print streams into the <tt>executeCommand()</tt> method of the command service (for a more detailed description of command services, see the next section).</p> |
| 162 | |
| 163 | <p>Notice that there is no method to add commands to the shell service |
| 164 | interface. This is because commands are implemented as OSGi services |
| 165 | and the shell service listens for service events and when a command |
| 166 | service registers/unregisters it automatically updates its list of |
| 167 | commands accordingly.</p> |
| 168 | |
| 169 | <p><a name="ApacheFelixShellService-commands"></a></p> |
| 170 | |
| 171 | <h2><a name="ApacheFelixShellService-HowCommandsWork"></a>How Commands Work</h2> |
| 172 | |
| 173 | <p>All commands available in the shell service are implemented as OSGi |
| 174 | services. The advantage of this approach is two-fold: the shell service |
| 175 | can leverage OSGi service events to maintain its list of available |
| 176 | commands and the set available commands is dynamically extendable by |
| 177 | installed bundles. The command service interface is defined as follows:</p> |
| 178 | |
Karl Pauls | 6450827 | 2009-09-06 21:15:38 +0000 | [diff] [blame^] | 179 | <div class="code"> |
| 180 | <textarea name="newcodemacro" class="java:nocontrols:nogutter" rows="10" readonly="readonly">package org.apache.felix.shell; |
Richard S. Hall | 677f589 | 2007-07-10 15:29:11 +0000 | [diff] [blame] | 181 | |
Karl Pauls | 6450827 | 2009-09-06 21:15:38 +0000 | [diff] [blame^] | 182 | public interface Command |
Richard S. Hall | 677f589 | 2007-07-10 15:29:11 +0000 | [diff] [blame] | 183 | { |
Karl Pauls | 6450827 | 2009-09-06 21:15:38 +0000 | [diff] [blame^] | 184 | public String getName(); |
| 185 | public String getUsage(); |
| 186 | public String getShortDescription(); |
| 187 | public void execute(String line, PrintStream out, PrintStream err); |
| 188 | }</textarea> |
| 189 | <script class="javascript"> |
| 190 | if(!window.newcodemacro_initialised) |
| 191 | { |
| 192 | window.newcodemacro_initialised = true; |
| 193 | window.oldonloadmethod = window.onload; |
| 194 | window.onload = function(){ |
| 195 | dp.SyntaxHighlighter.HighlightAll('newcodemacro'); |
| 196 | if(window.oldonloadmethod) |
| 197 | { |
| 198 | window.oldonloadmethod(); |
| 199 | } |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | </script> |
| 204 | </div> |
| 205 | |
Richard S. Hall | 677f589 | 2007-07-10 15:29:11 +0000 | [diff] [blame] | 206 | |
| 207 | <p>The semantics of the command service methods are:</p> |
| 208 | |
| 209 | <ul> |
| 210 | <li><tt>getName()</tt> - returns the name of the command; this must not contain whitespace and must be unique.</li> |
| 211 | <li><tt>getUsage()</tt> |
| 212 | - returns the usage string of the command; this should be one line and |
| 213 | as short as possible (this is used for generating the help command |
| 214 | output).</li> |
| 215 | <li><tt>getShortDescription()</tt> - returns a short |
| 216 | description of the command; this should be one line and as short as |
| 217 | possible (this is used for generating the help command output).</li> |
| 218 | <li><tt>execute()</tt> - executes the command's functionality using supplied command line and print streams.</li> |
| 219 | </ul> |
| 220 | |
| 221 | |
| 222 | <p><a name="ApacheFelixShellService-creating"></a></p> |
| 223 | |
| 224 | <h2><a name="ApacheFelixShellService-CreatingaCommand"></a>Creating a Command</h2> |
| 225 | |
| 226 | <p>The following example creates a simple version of the <tt>start</tt> command.</p> |
| 227 | |
Karl Pauls | 6450827 | 2009-09-06 21:15:38 +0000 | [diff] [blame^] | 228 | <div class="code"> |
| 229 | <textarea name="newcodemacro" class="java:nocontrols:nogutter" rows="10" readonly="readonly">package test; |
Richard S. Hall | 677f589 | 2007-07-10 15:29:11 +0000 | [diff] [blame] | 230 | |
Karl Pauls | 6450827 | 2009-09-06 21:15:38 +0000 | [diff] [blame^] | 231 | import java.io.PrintStream; |
| 232 | import java.net.URL; |
| 233 | import java.net.MalformedURLException; |
| 234 | import java.util.StringTokenizer; |
Richard S. Hall | 677f589 | 2007-07-10 15:29:11 +0000 | [diff] [blame] | 235 | |
Karl Pauls | 6450827 | 2009-09-06 21:15:38 +0000 | [diff] [blame^] | 236 | import org.osgi.framework.*; |
| 237 | import org.apache.felix.shell.ShellService; |
| 238 | import org.apache.felix.shell.Command; |
Richard S. Hall | 677f589 | 2007-07-10 15:29:11 +0000 | [diff] [blame] | 239 | |
Karl Pauls | 6450827 | 2009-09-06 21:15:38 +0000 | [diff] [blame^] | 240 | public class MyStartCommandImpl implements Command |
Richard S. Hall | 677f589 | 2007-07-10 15:29:11 +0000 | [diff] [blame] | 241 | { |
Karl Pauls | 6450827 | 2009-09-06 21:15:38 +0000 | [diff] [blame^] | 242 | private BundleContext m_context = null; |
Richard S. Hall | 677f589 | 2007-07-10 15:29:11 +0000 | [diff] [blame] | 243 | |
Karl Pauls | 6450827 | 2009-09-06 21:15:38 +0000 | [diff] [blame^] | 244 | public MyStartCommandImpl(BundleContext context) |
Richard S. Hall | 677f589 | 2007-07-10 15:29:11 +0000 | [diff] [blame] | 245 | { |
| 246 | m_context = context; |
| 247 | } |
| 248 | |
Karl Pauls | 6450827 | 2009-09-06 21:15:38 +0000 | [diff] [blame^] | 249 | public String getName() |
Richard S. Hall | 677f589 | 2007-07-10 15:29:11 +0000 | [diff] [blame] | 250 | { |
Karl Pauls | 6450827 | 2009-09-06 21:15:38 +0000 | [diff] [blame^] | 251 | return "mystart"; |
Richard S. Hall | 677f589 | 2007-07-10 15:29:11 +0000 | [diff] [blame] | 252 | } |
| 253 | |
Karl Pauls | 6450827 | 2009-09-06 21:15:38 +0000 | [diff] [blame^] | 254 | public String getUsage() |
Richard S. Hall | 677f589 | 2007-07-10 15:29:11 +0000 | [diff] [blame] | 255 | { |
Karl Pauls | 6450827 | 2009-09-06 21:15:38 +0000 | [diff] [blame^] | 256 | return "mystart <id> [<id> ...]"; |
Richard S. Hall | 677f589 | 2007-07-10 15:29:11 +0000 | [diff] [blame] | 257 | } |
| 258 | |
Karl Pauls | 6450827 | 2009-09-06 21:15:38 +0000 | [diff] [blame^] | 259 | public String getShortDescription() |
Richard S. Hall | 677f589 | 2007-07-10 15:29:11 +0000 | [diff] [blame] | 260 | { |
Karl Pauls | 6450827 | 2009-09-06 21:15:38 +0000 | [diff] [blame^] | 261 | return "start bundle(s)."; |
Richard S. Hall | 677f589 | 2007-07-10 15:29:11 +0000 | [diff] [blame] | 262 | } |
| 263 | |
Karl Pauls | 6450827 | 2009-09-06 21:15:38 +0000 | [diff] [blame^] | 264 | public void execute(String s, PrintStream out, PrintStream err) |
Richard S. Hall | 677f589 | 2007-07-10 15:29:11 +0000 | [diff] [blame] | 265 | { |
Karl Pauls | 6450827 | 2009-09-06 21:15:38 +0000 | [diff] [blame^] | 266 | StringTokenizer st = new StringTokenizer(s, " "); |
Richard S. Hall | 677f589 | 2007-07-10 15:29:11 +0000 | [diff] [blame] | 267 | |
Karl Pauls | 6450827 | 2009-09-06 21:15:38 +0000 | [diff] [blame^] | 268 | // Ignore the command name. |
| 269 | st.nextToken(); |
Richard S. Hall | 677f589 | 2007-07-10 15:29:11 +0000 | [diff] [blame] | 270 | |
Karl Pauls | 6450827 | 2009-09-06 21:15:38 +0000 | [diff] [blame^] | 271 | // There should be at least one bundle id. |
| 272 | if (st.countTokens() >= 1) |
Richard S. Hall | 677f589 | 2007-07-10 15:29:11 +0000 | [diff] [blame] | 273 | { |
Karl Pauls | 6450827 | 2009-09-06 21:15:38 +0000 | [diff] [blame^] | 274 | while (st.hasMoreTokens()) |
Richard S. Hall | 677f589 | 2007-07-10 15:29:11 +0000 | [diff] [blame] | 275 | { |
Karl Pauls | 6450827 | 2009-09-06 21:15:38 +0000 | [diff] [blame^] | 276 | String id = st.nextToken().trim(); |
Richard S. Hall | 677f589 | 2007-07-10 15:29:11 +0000 | [diff] [blame] | 277 | |
Karl Pauls | 6450827 | 2009-09-06 21:15:38 +0000 | [diff] [blame^] | 278 | try { |
| 279 | long l = Long.valueOf(id).longValue(); |
Richard S. Hall | 677f589 | 2007-07-10 15:29:11 +0000 | [diff] [blame] | 280 | Bundle bundle = m_context.getBundle(l); |
Karl Pauls | 6450827 | 2009-09-06 21:15:38 +0000 | [diff] [blame^] | 281 | if (bundle != null) |
Richard S. Hall | 677f589 | 2007-07-10 15:29:11 +0000 | [diff] [blame] | 282 | { |
| 283 | bundle.start(); |
| 284 | } |
Karl Pauls | 6450827 | 2009-09-06 21:15:38 +0000 | [diff] [blame^] | 285 | else |
Richard S. Hall | 677f589 | 2007-07-10 15:29:11 +0000 | [diff] [blame] | 286 | { |
Karl Pauls | 6450827 | 2009-09-06 21:15:38 +0000 | [diff] [blame^] | 287 | err.println("Bundle ID " + id + " is invalid."); |
Richard S. Hall | 677f589 | 2007-07-10 15:29:11 +0000 | [diff] [blame] | 288 | } |
Karl Pauls | 6450827 | 2009-09-06 21:15:38 +0000 | [diff] [blame^] | 289 | } catch (NumberFormatException ex) { |
| 290 | err.println("Unable to parse id '" + id + "'."); |
| 291 | } catch (BundleException ex) { |
| 292 | if (ex.getNestedException() != null) |
Richard S. Hall | 677f589 | 2007-07-10 15:29:11 +0000 | [diff] [blame] | 293 | err.println(ex.getNestedException().toString()); |
Karl Pauls | 6450827 | 2009-09-06 21:15:38 +0000 | [diff] [blame^] | 294 | else |
Richard S. Hall | 677f589 | 2007-07-10 15:29:11 +0000 | [diff] [blame] | 295 | err.println(ex.toString()); |
Karl Pauls | 6450827 | 2009-09-06 21:15:38 +0000 | [diff] [blame^] | 296 | } catch (Exception ex) { |
Richard S. Hall | 677f589 | 2007-07-10 15:29:11 +0000 | [diff] [blame] | 297 | err.println(ex.toString()); |
| 298 | } |
| 299 | } |
| 300 | } |
Karl Pauls | 6450827 | 2009-09-06 21:15:38 +0000 | [diff] [blame^] | 301 | else |
Richard S. Hall | 677f589 | 2007-07-10 15:29:11 +0000 | [diff] [blame] | 302 | { |
Karl Pauls | 6450827 | 2009-09-06 21:15:38 +0000 | [diff] [blame^] | 303 | err.println("Incorrect number of arguments"); |
Richard S. Hall | 677f589 | 2007-07-10 15:29:11 +0000 | [diff] [blame] | 304 | } |
| 305 | } |
Karl Pauls | 6450827 | 2009-09-06 21:15:38 +0000 | [diff] [blame^] | 306 | }</textarea> |
| 307 | <script class="javascript"> |
| 308 | if(!window.newcodemacro_initialised) |
| 309 | { |
| 310 | window.newcodemacro_initialised = true; |
| 311 | window.oldonloadmethod = window.onload; |
| 312 | window.onload = function(){ |
| 313 | dp.SyntaxHighlighter.HighlightAll('newcodemacro'); |
| 314 | if(window.oldonloadmethod) |
| 315 | { |
| 316 | window.oldonloadmethod(); |
| 317 | } |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | </script> |
| 322 | </div> |
| 323 | |
Richard S. Hall | 677f589 | 2007-07-10 15:29:11 +0000 | [diff] [blame] | 324 | |
| 325 | <p>A bundle activator class is needed for packaging the command servce; the bundle activator registers the command service in its <tt>start()</tt> method. Note: You do not need one activator per command, a single activator can register any number of commands.</p> |
| 326 | |
Karl Pauls | 6450827 | 2009-09-06 21:15:38 +0000 | [diff] [blame^] | 327 | <div class="code"> |
| 328 | <textarea name="newcodemacro" class="java:nocontrols:nogutter" rows="10" readonly="readonly">package test; |
Richard S. Hall | 677f589 | 2007-07-10 15:29:11 +0000 | [diff] [blame] | 329 | |
Karl Pauls | 6450827 | 2009-09-06 21:15:38 +0000 | [diff] [blame^] | 330 | import org.osgi.framework.BundleActivator; |
| 331 | import org.osgi.framework.BundleContext; |
Richard S. Hall | 677f589 | 2007-07-10 15:29:11 +0000 | [diff] [blame] | 332 | |
Karl Pauls | 6450827 | 2009-09-06 21:15:38 +0000 | [diff] [blame^] | 333 | public class MyStartActivator implements BundleActivator |
Richard S. Hall | 677f589 | 2007-07-10 15:29:11 +0000 | [diff] [blame] | 334 | { |
Karl Pauls | 6450827 | 2009-09-06 21:15:38 +0000 | [diff] [blame^] | 335 | private transient BundleContext m_context = null; |
Richard S. Hall | 677f589 | 2007-07-10 15:29:11 +0000 | [diff] [blame] | 336 | |
Karl Pauls | 6450827 | 2009-09-06 21:15:38 +0000 | [diff] [blame^] | 337 | public void start(BundleContext context) |
Richard S. Hall | 677f589 | 2007-07-10 15:29:11 +0000 | [diff] [blame] | 338 | { |
| 339 | m_context = context; |
| 340 | |
Karl Pauls | 6450827 | 2009-09-06 21:15:38 +0000 | [diff] [blame^] | 341 | // Register the command service. |
| 342 | context.registerService( |
Richard S. Hall | 677f589 | 2007-07-10 15:29:11 +0000 | [diff] [blame] | 343 | org.apache.felix.shell.Command.class.getName(), |
Karl Pauls | 6450827 | 2009-09-06 21:15:38 +0000 | [diff] [blame^] | 344 | new MyStartCommandImpl(m_context), null); |
Richard S. Hall | 677f589 | 2007-07-10 15:29:11 +0000 | [diff] [blame] | 345 | } |
| 346 | |
Karl Pauls | 6450827 | 2009-09-06 21:15:38 +0000 | [diff] [blame^] | 347 | public void stop(BundleContext context) |
Richard S. Hall | 677f589 | 2007-07-10 15:29:11 +0000 | [diff] [blame] | 348 | { |
Karl Pauls | 6450827 | 2009-09-06 21:15:38 +0000 | [diff] [blame^] | 349 | // Services are automatically unregistered so |
| 350 | // we don't have to unregister the factory here. |
| 351 | } |
| 352 | }</textarea> |
| 353 | <script class="javascript"> |
| 354 | if(!window.newcodemacro_initialised) |
| 355 | { |
| 356 | window.newcodemacro_initialised = true; |
| 357 | window.oldonloadmethod = window.onload; |
| 358 | window.onload = function(){ |
| 359 | dp.SyntaxHighlighter.HighlightAll('newcodemacro'); |
| 360 | if(window.oldonloadmethod) |
| 361 | { |
| 362 | window.oldonloadmethod(); |
| 363 | } |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | </script> |
| 368 | </div> |
| 369 | |
Richard S. Hall | 677f589 | 2007-07-10 15:29:11 +0000 | [diff] [blame] | 370 | |
| 371 | <p>To compile these classes you will need to have the <tt>framework.jar</tt> file on your class path. Compile all of the source files using a command like:</p> |
| 372 | |
Karl Pauls | 6450827 | 2009-09-06 21:15:38 +0000 | [diff] [blame^] | 373 | <div class="preformatted panel" style="border-width: 1px;"><div class="preformattedContent panelContent"> |
Richard S. Hall | 677f589 | 2007-07-10 15:29:11 +0000 | [diff] [blame] | 374 | <pre>java -d c:\classes *.java |
| 375 | </pre> |
| 376 | </div></div> |
| 377 | |
| 378 | <p>This command compiles all of the source files and outputs the generated class files into a subdirectory of the <tt>c:\classes</tt> directory, called test, named after the package of the source files; for the above command to work, the <tt>c:\classes</tt> |
| 379 | directory must exist. Once you have compiled all of the above classes, |
| 380 | you need to create a bundle JAR file of the generated package |
| 381 | directory. The bundle JAR file needs a manifest, so create a file |
| 382 | called <tt>manifest.mf</tt> with the following contents:</p> |
| 383 | |
Karl Pauls | 6450827 | 2009-09-06 21:15:38 +0000 | [diff] [blame^] | 384 | <div class="code"> |
| 385 | <textarea name="newcodemacro" class="java:nocontrols:nogutter" rows="10" readonly="readonly">Bundle-Name: My Start Command |
| 386 | Bundle-Description: A 'start' command for the shell service. |
Richard S. Hall | 677f589 | 2007-07-10 15:29:11 +0000 | [diff] [blame] | 387 | Bundle-Activator: test.MyStartActivator |
| 388 | Bundle-ClassPath: . |
Karl Pauls | 6450827 | 2009-09-06 21:15:38 +0000 | [diff] [blame^] | 389 | Import-Package: org.apache.felix.shell</textarea> |
| 390 | <script class="javascript"> |
| 391 | if(!window.newcodemacro_initialised) |
| 392 | { |
| 393 | window.newcodemacro_initialised = true; |
| 394 | window.oldonloadmethod = window.onload; |
| 395 | window.onload = function(){ |
| 396 | dp.SyntaxHighlighter.HighlightAll('newcodemacro'); |
| 397 | if(window.oldonloadmethod) |
| 398 | { |
| 399 | window.oldonloadmethod(); |
| 400 | } |
| 401 | } |
| 402 | } |
| 403 | |
| 404 | </script> |
| 405 | </div> |
| 406 | |
Richard S. Hall | 677f589 | 2007-07-10 15:29:11 +0000 | [diff] [blame] | 407 | |
| 408 | <p>To create the bundle JAR file, issue the command:</p> |
| 409 | |
Karl Pauls | 6450827 | 2009-09-06 21:15:38 +0000 | [diff] [blame^] | 410 | <div class="preformatted panel" style="border-width: 1px;"><div class="preformattedContent panelContent"> |
Richard S. Hall | 677f589 | 2007-07-10 15:29:11 +0000 | [diff] [blame] | 411 | <pre>jar cfm mystart.jar manifest.mf -C c:\classes test |
| 412 | </pre> |
| 413 | </div></div> |
| 414 | |
| 415 | <p>This command creates a JAR file using the manifest you created and |
| 416 | includes all of the classes in the test directory inside of the <tt>c:\classes</tt> |
| 417 | directory. Once the bundle JAR file is created, you are ready to add |
| 418 | the command service to the shell service; simply start Felix and |
| 419 | install and start the bundle created by the above command. By doing so, |
| 420 | the new <tt>mystart</tt> command is made available via the shell service.</p> |
| 421 | |
| 422 | <p><a name="ApacheFelixShellService-security"></a></p> |
| 423 | |
| 424 | <h2><a name="ApacheFelixShellService-SecurityandtheShellService"></a>Security and the Shell Service</h2> |
| 425 | |
| 426 | <p>The shell service security handling is quite simple, all security is |
| 427 | handled by the standard OSGi framework mechanisms. For example, if a |
| 428 | bundle should not be able to register a shell service, then it should |
| 429 | not be given the corresponding service permission. Security handling |
| 430 | may change in future release after some experience is gained through |
| 431 | usage.</p> |
| 432 | |
| 433 | <p><a name="ApacheFelixShellService-feedback"></a></p> |
| 434 | |
| 435 | <h2><a name="ApacheFelixShellService-Feedback"></a>Feedback</h2> |
| 436 | |
Karl Pauls | 6450827 | 2009-09-06 21:15:38 +0000 | [diff] [blame^] | 437 | <p>Subscribe to the Felix users mailing list by sending a message to <a href="mailto:users-subscribe@felix.apache.org" rel="nofollow">users-subscribe@felix.apache.org</a>; after subscribing, email questions or feedback to <a href="mailto:users@felix.apache.org" rel="nofollow">users@felix.apache.org</a>.</p> |
Richard S. Hall | 677f589 | 2007-07-10 15:29:11 +0000 | [diff] [blame] | 438 | </div> |
Karl Pauls | 6450827 | 2009-09-06 21:15:38 +0000 | [diff] [blame^] | 439 | </body></html> |