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