1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 package oooapplet;
23 
24 import java.lang.reflect.Method;
25 import java.lang.reflect.Array;
26 import java.net.*;
27 import java.io.*;
28 import java.awt.*;
29 import java.awt.event.*;
30 import com.sun.star.comp.beans.*;
31 import java.applet.Applet;
32 import java.awt.Graphics;
33 import java.util.*;
34 
35 public class OOoViewer extends Applet {
36 
37     private OOoBean oBean;
38 
39     static private CustomURLClassLoader m_loader;
40 
41     Object  m_objBean;
42 
init()43     public void init() {
44         try {
45             if (m_loader == null) {
46                 String s = getParameter("office");
47                 System.out.println("sun.awt.noxembed: " + System.getProperty("sun.awt.noxembed"));
48                 System.setProperty("sun.awt.xembedserver", "true");
49 
50                 File f = new File(s);
51                 URL url = f.toURL();
52                 String officeURL = url.toString();
53                 URL[] arURL = new URL[] {
54                     new URL(officeURL + "/program/classes/officebean.jar"),
55                     new URL(officeURL + "/program/classes/jurt.jar"),
56                     new URL(officeURL + "/program/classes/ridl.jar"),
57                     new URL(officeURL + "/program/classes/unoil.jar"),
58                     new URL(officeURL + "/program/classes/java_uno.jar"),
59                     new URL(officeURL + "/program/classes/juh.jar")
60                 };
61                 m_loader = new CustomURLClassLoader(arURL);
62                 File fileProg = new File(s + "/program");
63                 m_loader.addResourcePath(fileProg.toURL());
64             }
65         } catch (MalformedURLException e) {
66             e.printStackTrace();
67         }
68     }
69 
start()70     public void start() {
71         try {
72         Class beanClass = m_loader.loadClass("com.sun.star.comp.beans.OOoBean");
73         m_objBean = beanClass.newInstance();
74         setLayout(new BorderLayout());
75         add((java.awt.Container)m_objBean, BorderLayout.CENTER);
76         setVisible(true);
77         //this does not work here. Why?
78 //        Class arPropValClass = m_loader.loadClass("[Lcom.sun.star.beans.PropertyValue;");
79         Object arProp = Array.newInstance(
80             m_loader.loadClass("com.sun.star.beans.PropertyValue"), 1);
81         Class clazz = arProp.getClass();
82 
83         Method methLoad = beanClass.getMethod(
84             "loadFromURL", new Class[] {
85                 String.class, arProp.getClass() });
86 
87         methLoad.invoke(m_objBean, new Object[] {"private:factory/swriter", null});
88         } catch (ClassNotFoundException e) {
89             e.printStackTrace();
90         } catch (InstantiationException e) {
91             e.printStackTrace();
92         } catch (IllegalAccessException e) {
93             e.printStackTrace();
94         } catch (ClassCastException e) {
95             e.printStackTrace();
96         } catch (java.lang.reflect.InvocationTargetException e) {
97             e.printStackTrace();
98         } catch (java.lang.NoSuchMethodException e) {
99             e.printStackTrace();        }
100 
101 
102 
103         validate();
104     }
105 
stop()106     public void stop() {
107         try {
108             Method methStop = m_objBean.getClass().getMethod(
109                 "stopOOoConnection", new Class[0]);
110             methStop.invoke(m_objBean, null);
111         } catch (java.lang.NoSuchMethodException e) {
112             e.printStackTrace();
113         } catch (java.lang.IllegalAccessException e) {
114             e.printStackTrace();
115         }
116          catch (java.lang.reflect.InvocationTargetException e) {
117             e.printStackTrace();
118          }
119 
120     }
121 
destroy()122     public void destroy() {
123     }
124 
paint(Graphics g)125     public void paint(Graphics g) {
126     }
127 }
128 
129 
130 final class CustomURLClassLoader extends URLClassLoader {
131 
132     private Vector resourcePaths;
133 
CustomURLClassLoader( URL[] urls )134     public CustomURLClassLoader( URL[] urls ) {
135         super( urls );
136     }
137 
findClass( String name )138     protected Class findClass( String name ) throws ClassNotFoundException {
139         // This is only called via this.loadClass -> super.loadClass ->
140         // this.findClass, after this.loadClass has already called
141         // super.findClass, so no need to call super.findClass again:
142         throw new ClassNotFoundException( name );
143 //        return super.findClass(name);
144     }
145 
146 
147 
loadClass( String name, boolean resolve )148     protected Class loadClass( String name, boolean resolve )
149         throws ClassNotFoundException
150     {
151         Class c = findLoadedClass( name );
152         if ( c == null ) {
153             try {
154                 c = super.findClass( name );
155             } catch ( ClassNotFoundException e ) {
156                 return super.loadClass( name, resolve );
157             } catch ( SecurityException e ) {
158                 // A SecurityException "Prohibited package name: java.lang"
159                 // may occur when the user added the JVM's rt.jar to the
160                 // java.class.path:
161                 return super.loadClass( name, resolve );
162             }
163         }
164         if ( resolve ) {
165             resolveClass( c );
166         }
167         return c;
168     }
169 
addResourcePath(URL rurl)170     public void addResourcePath(URL rurl) {
171         if (resourcePaths == null) resourcePaths = new Vector();
172         resourcePaths.add(rurl);
173     }
174 
getResource(String name)175     public URL getResource(String name) {
176         if (resourcePaths == null) return null;
177 
178         URL result = super.getResource(name);
179         if (result != null) {
180             return result;
181         }
182 
183         URL u = null;
184         URI uri = null;
185         for (Enumeration e = resourcePaths.elements(); e.hasMoreElements();) {
186             u = (URL)e.nextElement();
187             if (u.getProtocol().startsWith("file")){
188                 try {
189                     File f1 = new File(u.getPath());
190                     File f2 = new File(f1, name);
191                     if (f2.exists()) {
192                         return new URL(f2.toURI().toASCIIString());
193                     }
194                 } catch (MalformedURLException e1) {
195                     System.err.println("malformed url: "+e1.getMessage());
196                     continue;
197                 }
198             }
199         }
200         return null;
201     }
202 
203 }
204