KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ungoverned > osgi > bundle > simple > SimpleBundle


1 /*
2  * Simple Example Bundle
3  * Copyright (c) 2004, Richard S. Hall
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * * Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in
14  * the documentation and/or other materials provided with the
15  * distribution.
16  * * Neither the name of the ungoverned.org nor the names of its
17  * contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  * Contact: Richard S. Hall (heavy@ungoverned.org)
33  * Contributor(s):
34  *
35 **/

36 package org.ungoverned.osgi.bundle.simple;
37
38 import javax.servlet.Servlet JavaDoc;
39
40 import org.osgi.framework.BundleActivator;
41 import org.osgi.framework.BundleContext;
42 import org.ungoverned.osgi.bundle.simple.embedded.Embedded;
43
44 /**
45  * A very simple bundle that prints out a message when it is started and
46  * stopped; it includes an embedded JAR file which is used on its internal
47  * class path. This bundle is merely a "hello world" example; it does not
48  * implement any services nor does it offer any configurable properties.
49 **/

50 public class SimpleBundle implements BundleActivator
51 {
52     private BundleContext context = null;
53
54     public native String JavaDoc foo();
55
56     public void start(BundleContext context) throws Exception JavaDoc
57     {
58         System.out.println("Simple Bundle " + context.getBundle().getBundleId()
59             + " has started.");
60
61         this.context = context;
62
63         // Get the OS and processor properties.
64
String JavaDoc os =
65             context.getProperty("org.osgi.framework.os.name").toLowerCase();
66         String JavaDoc processor =
67             context.getProperty("org.osgi.framework.processor").toLowerCase();
68
69         // Load library if correct platform.
70
if (os.equals("linux") && processor.endsWith("86"))
71         {
72             try {
73                 System.loadLibrary("foo");
74             } catch (Exception JavaDoc ex) {
75                 System.out.println("No library: " + ex);
76                 ex.printStackTrace();
77             }
78             System.out.println("From native: " + foo());
79         }
80
81         // Create class from embedded JAR file.
82
Embedded embedded = new Embedded();
83         embedded.sayHello();
84
85         // Access dynamically imported servlet class.
86
try {
87             System.out.println("Resource = " + getClass().getResource("/javax/servlet/Servlet.class"));
88         } catch (Throwable JavaDoc ex) {
89             System.out.println("The 'javax.servlet' package is not available.");
90         }
91         try {
92             System.out.println("Class name = " + javax.servlet.http.HttpSession JavaDoc.class);
93         } catch (Throwable JavaDoc ex) {
94             System.out.println("The 'javax.servlet.http' package is not available.");
95         }
96         try {
97             System.out.println("Class name = " + Servlet JavaDoc.class);
98         } catch (Throwable JavaDoc ex) {
99             System.out.println("The 'javax.servlet' package is not available.");
100         }
101     }
102
103     public void stop(BundleContext context)
104     {
105         System.out.println("Simple Bundle " + context.getBundle().getBundleId()
106             + " has stopped.");
107     }
108 }
109
Popular Tags