Class | Line # | Actions | |||||
---|---|---|---|---|---|---|---|
System | 35 | 17 | 0% | 10 | 5 |
1 | /* | |
2 | * See the NOTICE file distributed with this work for additional | |
3 | * information regarding copyright ownership. | |
4 | * | |
5 | * This is free software; you can redistribute it and/or modify it | |
6 | * under the terms of the GNU Lesser General Public License as | |
7 | * published by the Free Software Foundation; either version 2.1 of | |
8 | * the License, or (at your option) any later version. | |
9 | * | |
10 | * This software is distributed in the hope that it will be useful, | |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 | * Lesser General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU Lesser General Public | |
16 | * License along with this software; if not, write to the Free | |
17 | * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA | |
18 | * 02110-1301 USA, or see the FSF site: http://www.fsf.org. | |
19 | */ | |
20 | package org.xwiki.environment; | |
21 | ||
22 | import java.io.File; | |
23 | ||
24 | import org.xwiki.component.embed.EmbeddableComponentManager; | |
25 | import org.xwiki.component.manager.ComponentLookupException; | |
26 | import org.xwiki.component.manager.ComponentManager; | |
27 | import org.xwiki.environment.internal.StandardEnvironment; | |
28 | ||
29 | /** | |
30 | * Helper class that can be used to easily initialize the full XWiki System (ie Component Manager and Environment). | |
31 | * | |
32 | * @version $Id: 0e77d521be8bb24160e1ed50fa2da432ca763471 $ | |
33 | * @since 3.5M1 | |
34 | */ | |
35 | public final class System | |
36 | { | |
37 | /** | |
38 | * Helper static class, no public constructor. | |
39 | */ | |
40 | 0 | private System() |
41 | { | |
42 | // Prevent instantiation, this is a helper class. | |
43 | } | |
44 | ||
45 | /** | |
46 | * Initialize the full XWiki system (ie Component Manager and Environment), using the "java.io.tmpdir" System | |
47 | * property value as the temporary diretory location, without any resource directory set and without any permanent | |
48 | * directory set either. | |
49 | * | |
50 | * @param classLoader see {@link EmbeddableComponentManager#initialize(ClassLoader)} | |
51 | * @return the initialized Component Manager | |
52 | * @see #dispose(ComponentManager) | |
53 | */ | |
54 | 0 | public static ComponentManager initialize(ClassLoader classLoader) |
55 | { | |
56 | 0 | return initialize(null, null, null, classLoader); |
57 | } | |
58 | ||
59 | /** | |
60 | * Initialize the full XWiki system (ie Component Manager and Environment), using the class loader that loaded | |
61 | * this class, using the "java.io.tmpdir" System property value as the temporary diretory location, without | |
62 | * any resource directory set and without any permanent directory set either. | |
63 | * | |
64 | * @return the initialized Component Manager | |
65 | * @see #dispose(ComponentManager) | |
66 | */ | |
67 | 30 | public static ComponentManager initialize() |
68 | { | |
69 | 30 | return initialize((File) null); |
70 | } | |
71 | ||
72 | /** | |
73 | * Initialize the full XWiki system (ie Component Manager and Environment), using the class loader that loaded | |
74 | * this class, using the "java.io.tmpdir" System property value as the temporary diretory location and without | |
75 | * any resource directory set (the permanent directory will be used as the resource directory). | |
76 | * | |
77 | * @param permanentDirectory see {@link org.xwiki.environment.Environment#getPermanentDirectory()} | |
78 | * @return the initialized Component Manager | |
79 | * @see #dispose(ComponentManager) | |
80 | */ | |
81 | 30 | public static ComponentManager initialize(File permanentDirectory) |
82 | { | |
83 | 30 | return initialize(permanentDirectory, null); |
84 | } | |
85 | ||
86 | /** | |
87 | * Initialize the full XWiki system (ie Component Manager and Environment), using the class loader that loaded | |
88 | * this class and using the "java.io.tmpdir" System property value as the temporary diretory location. | |
89 | * | |
90 | * @param permanentDirectory see {@link org.xwiki.environment.Environment#getPermanentDirectory()} | |
91 | * @param resourceDirectory see | |
92 | * {@link org.xwiki.environment.internal.StandardEnvironment#setResourceDirectory(java.io.File)} | |
93 | * @return the initialized Component Manager | |
94 | * @see #dispose(ComponentManager) | |
95 | */ | |
96 | 30 | public static ComponentManager initialize(File permanentDirectory, File resourceDirectory) |
97 | { | |
98 | 30 | return initialize(permanentDirectory, resourceDirectory, null); |
99 | } | |
100 | ||
101 | /** | |
102 | * Initialize the full XWiki system (ie Component Manager and Environment), using the class loader that loaded | |
103 | * this class. | |
104 | * | |
105 | * @param permanentDirectory see {@link org.xwiki.environment.Environment#getPermanentDirectory()} | |
106 | * @param resourceDirectory see | |
107 | * {@link org.xwiki.environment.internal.StandardEnvironment#setResourceDirectory(java.io.File)} | |
108 | * @param temporaryDirectory see {@link org.xwiki.environment.Environment#getTemporaryDirectory()} | |
109 | * @return the initialized Component Manager | |
110 | * @see #dispose(ComponentManager) | |
111 | */ | |
112 | 31 | public static ComponentManager initialize(File permanentDirectory, File resourceDirectory, File temporaryDirectory) |
113 | { | |
114 | 31 | return initialize(permanentDirectory, resourceDirectory, temporaryDirectory, null); |
115 | } | |
116 | ||
117 | /** | |
118 | * Initialize the full XWiki system (ie Component Manager and Environment). | |
119 | * | |
120 | * @param permanentDirectory see {@link org.xwiki.environment.Environment#getPermanentDirectory()} | |
121 | * @param resourceDirectory see | |
122 | * {@link org.xwiki.environment.internal.StandardEnvironment#setResourceDirectory(java.io.File)} | |
123 | * @param temporaryDirectory see {@link org.xwiki.environment.Environment#getTemporaryDirectory()} | |
124 | * @param classLoader see {@link EmbeddableComponentManager#initialize(ClassLoader)} | |
125 | * @return the initialized Component Manager | |
126 | * @see #dispose(ComponentManager) | |
127 | */ | |
128 | 31 | public static ComponentManager initialize(File permanentDirectory, File resourceDirectory, File temporaryDirectory, |
129 | ClassLoader classLoader) | |
130 | { | |
131 | // Step 1: Initialize Component system | |
132 | 31 | EmbeddableComponentManager ecm = new EmbeddableComponentManager(); |
133 | 31 | ecm.initialize(classLoader == null ? System.class.getClassLoader() : classLoader); |
134 | ||
135 | // Step 2: Initialize Environment | |
136 | 31 | StandardEnvironment environment; |
137 | 31 | try { |
138 | 31 | environment = ecm.getInstance(Environment.class); |
139 | } catch (ComponentLookupException e) { | |
140 | 0 | throw new RuntimeException("Failed to find Standard Environment", e); |
141 | } | |
142 | 31 | environment.setPermanentDirectory(permanentDirectory); |
143 | 31 | environment.setResourceDirectory(resourceDirectory); |
144 | 31 | environment.setTemporaryDirectory(temporaryDirectory); |
145 | ||
146 | 31 | return ecm; |
147 | } | |
148 | ||
149 | /** | |
150 | * Free resource taken by the Component Manager create by one of the <code>initialize</code> methods. | |
151 | * | |
152 | * @param componentManager the component manager | |
153 | * @since 5.1 | |
154 | */ | |
155 | 29 | public static void dispose(ComponentManager componentManager) |
156 | { | |
157 | 29 | EmbeddableComponentManager ecm = (EmbeddableComponentManager) componentManager; |
158 | 29 | ecm.dispose(); |
159 | } | |
160 | } |