1 //*************************************************************************
2 //
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26 //*************************************************************************
27 package oooapplet;
28 
29 import java.lang.reflect.Method;
30 import java.lang.reflect.Array;
31 import java.net.*;
32 import java.io.*;
33 import java.awt.*;
34 import java.awt.event.*;
35 import com.sun.star.comp.beans.*;
36 import java.applet.Applet;
37 import java.awt.Graphics;
38 import java.util.*;
39 
40 public class OOoViewer extends Applet {
41 
42     private OOoBean oBean;
43 
44     static private CustomURLClassLoader m_loader;
45 
46     Object  m_objBean;
47 
48     public void init() {
49         try {
50             if (m_loader == null) {
51                 String s = getParameter("office");
52                 System.out.println("sun.awt.noxembed: " + System.getProperty("sun.awt.noxembed"));
53                 System.setProperty("sun.awt.xembedserver", "true");
54 
55                 File f = new File(s);
56                 URL url = f.toURL();
57                 String officeURL = url.toString();
58                 URL[] arURL = new URL[] {
59                     new URL(officeURL + "/program/classes/officebean.jar"),
60                     new URL(officeURL + "/program/classes/jurt.jar"),
61                     new URL(officeURL + "/program/classes/ridl.jar"),
62                     new URL(officeURL + "/program/classes/unoil.jar"),
63                     new URL(officeURL + "/program/classes/java_uno.jar"),
64                     new URL(officeURL + "/program/classes/juh.jar")
65                 };
66                 m_loader = new CustomURLClassLoader(arURL);
67                 File fileProg = new File(s + "/program");
68                 m_loader.addResourcePath(fileProg.toURL());
69             }
70         } catch (MalformedURLException e) {
71             e.printStackTrace();
72         }
73     }
74 
75     public void start() {
76         try {
77         Class beanClass = m_loader.loadClass("com.sun.star.comp.beans.OOoBean");
78         m_objBean = beanClass.newInstance();
79         setLayout(new BorderLayout());
80         add((java.awt.Container)m_objBean, BorderLayout.CENTER);
81         setVisible(true);
82         //this does not work here. Why?
83 //        Class arPropValClass = m_loader.loadClass("[Lcom.sun.star.beans.PropertyValue;");
84         Object arProp = Array.newInstance(
85             m_loader.loadClass("com.sun.star.beans.PropertyValue"), 1);
86         Class clazz = arProp.getClass();
87 
88         Method methLoad = beanClass.getMethod(
89             "loadFromURL", new Class[] {
90                 String.class, arProp.getClass() });
91 
92         methLoad.invoke(m_objBean, new Object[] {"private:factory/swriter", null});
93         } catch (ClassNotFoundException e) {
94             e.printStackTrace();
95         } catch (InstantiationException e) {
96             e.printStackTrace();
97         } catch (IllegalAccessException e) {
98             e.printStackTrace();
99         } catch (ClassCastException e) {
100             e.printStackTrace();
101         } catch (java.lang.reflect.InvocationTargetException e) {
102             e.printStackTrace();
103         } catch (java.lang.NoSuchMethodException e) {
104             e.printStackTrace();        }
105 
106 
107 
108         validate();
109     }
110 
111     public void stop() {
112         try {
113             Method methStop = m_objBean.getClass().getMethod(
114                 "stopOOoConnection", new Class[0]);
115             methStop.invoke(m_objBean, null);
116         } catch (java.lang.NoSuchMethodException e) {
117             e.printStackTrace();
118         } catch (java.lang.IllegalAccessException e) {
119             e.printStackTrace();
120         }
121          catch (java.lang.reflect.InvocationTargetException e) {
122             e.printStackTrace();
123          }
124 
125     }
126 
127     public void destroy() {
128     }
129 
130     public void paint(Graphics g) {
131     }
132 }
133 
134 
135 final class CustomURLClassLoader extends URLClassLoader {
136 
137     private Vector resourcePaths;
138 
139     public CustomURLClassLoader( URL[] urls ) {
140         super( urls );
141     }
142 
143     protected Class findClass( String name ) throws ClassNotFoundException {
144         // This is only called via this.loadClass -> super.loadClass ->
145         // this.findClass, after this.loadClass has already called
146         // super.findClass, so no need to call super.findClass again:
147         throw new ClassNotFoundException( name );
148 //        return super.findClass(name);
149     }
150 
151 
152 
153     protected Class loadClass( String name, boolean resolve )
154         throws ClassNotFoundException
155     {
156         Class c = findLoadedClass( name );
157         if ( c == null ) {
158             try {
159                 c = super.findClass( name );
160             } catch ( ClassNotFoundException e ) {
161                 return super.loadClass( name, resolve );
162             } catch ( SecurityException e ) {
163                 // A SecurityException "Prohibited package name: java.lang"
164                 // may occur when the user added the JVM's rt.jar to the
165                 // java.class.path:
166                 return super.loadClass( name, resolve );
167             }
168         }
169         if ( resolve ) {
170             resolveClass( c );
171         }
172         return c;
173     }
174 
175     public void addResourcePath(URL rurl) {
176         if (resourcePaths == null) resourcePaths = new Vector();
177         resourcePaths.add(rurl);
178     }
179 
180     public URL getResource(String name) {
181         if (resourcePaths == null) return null;
182 
183         URL result = super.getResource(name);
184         if (result != null) {
185             return result;
186         }
187 
188         URL u = null;
189         URI uri = null;
190         for (Enumeration e = resourcePaths.elements(); e.hasMoreElements();) {
191             u = (URL)e.nextElement();
192             if (u.getProtocol().startsWith("file")){
193                 try {
194                     File f1 = new File(u.getPath());
195                     File f2 = new File(f1, name);
196                     if (f2.exists()) {
197                         return new URL(f2.toURI().toASCIIString());
198                     }
199                 } catch (MalformedURLException e1) {
200                     System.err.println("malformed url: "+e1.getMessage());
201                     continue;
202                 }
203             }
204         }
205         return null;
206     }
207 
208 }
209