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