xref: /trunk/main/scripting/workben/ifc/scripting/ScriptingUtils.java (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
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 
28 package ifc.script.framework;
29 
30 import java.io.File;
31 import drafts.com.sun.star.script.framework.storage.XScriptStorageManager;
32 
33 import com.sun.star.uno.UnoRuntime;
34 import com.sun.star.lang.XMultiServiceFactory;
35 import com.sun.star.uno.XInterface;
36 import com.sun.star.ucb.XSimpleFileAccess;
37 import com.sun.star.beans.XPropertySet;
38 import com.sun.star.uno.XComponentContext;
39 
40 public class ScriptingUtils {
41     private XScriptStorageManager storageManager;
42     private static ScriptingUtils utils;
43 
44     private ScriptingUtils() {
45     }
46 
47     public static ScriptingUtils getDefault() {
48         if (utils == null) {
49             synchronized (ScriptingUtils.class) {
50                 if (utils == null)
51                     utils = new ScriptingUtils();
52             }
53         }
54         return utils;
55     }
56 
57     public static void cleanUserDir() {
58     }
59 
60     public static void cleanShareDir() {
61     }
62 
63     public Object getScriptStorage(XMultiServiceFactory xMSF, String location) {
64         int id = getStorageId(xMSF, location);
65         return storageManager.getScriptStorage(id);
66     }
67 
68     public int getStorageId(XMultiServiceFactory xMSF, String location) {
69 
70         if (location.equals("share"))
71             return 0;
72 
73         if (location.equals("user"))
74             return 1;
75 
76         XSimpleFileAccess access = null;
77         String uri = util.utils.getFullTestURL(location);
78 
79         if (storageManager == null) {
80             try {
81                 XPropertySet xProp = (XPropertySet)UnoRuntime.queryInterface(
82                     XPropertySet.class, xMSF);
83 
84                 XComponentContext xContext = (XComponentContext)
85                     UnoRuntime.queryInterface(XComponentContext.class,
86                     xProp.getPropertyValue("DefaultContext"));
87 
88                 XInterface ifc = (XInterface)
89                     xContext.getValueByName("/singletons/drafts.com.sun.star." +
90                     "script.framework.storage.theScriptStorageManager");
91 
92                 storageManager = (XScriptStorageManager)
93                     UnoRuntime.queryInterface(XScriptStorageManager.class, ifc);
94             }
95             catch( Exception e ) {
96                 return -1;
97             }
98         }
99 
100         access = getXSimpleFileAccess(xMSF);
101         if (access == null)
102             return -1;
103 
104         int id = storageManager.createScriptStorageWithURI(access, uri);
105 
106         return id;
107     }
108 
109     public XSimpleFileAccess getXSimpleFileAccess(XMultiServiceFactory xMSF) {
110         XSimpleFileAccess access = null;
111 
112         try {
113             Object fa =
114                 xMSF.createInstance("com.sun.star.ucb.SimpleFileAccess");
115 
116             access = (XSimpleFileAccess)
117                 UnoRuntime.queryInterface(XSimpleFileAccess.class, fa);
118         }
119         catch (com.sun.star.uno.Exception e) {
120             return null;
121         }
122         return access;
123     }
124 }
125