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