1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31 package org.jomc.modlet;
32
33 import java.security.AccessController;
34 import java.security.PrivilegedAction;
35 import java.text.MessageFormat;
36 import java.util.Locale;
37 import java.util.ResourceBundle;
38
39
40
41
42
43
44
45
46 public abstract class ModelContextFactory
47 {
48
49
50 private static final String DEFAULT_MODEL_CONTEXT_FACTORY_CLASS_NAME = "org.jomc.modlet.DefaultModelContextFactory";
51
52
53 private static final String MODEL_CONTEXT_FACTORY_CLASS_NAME_PROPERTY =
54 "org.jomc.modlet.ModelContextFactory";
55
56
57 protected ModelContextFactory()
58 {
59 super();
60 }
61
62
63
64
65
66
67
68
69
70
71
72 public static ModelContextFactory newInstance() throws ModelContextFactoryError
73 {
74 return newInstance( AccessController.doPrivileged( new PrivilegedAction<String>()
75 {
76
77 public String run()
78 {
79 return System.getProperty( MODEL_CONTEXT_FACTORY_CLASS_NAME_PROPERTY,
80 DEFAULT_MODEL_CONTEXT_FACTORY_CLASS_NAME );
81
82 }
83
84 } ) );
85 }
86
87
88
89
90
91
92
93
94
95
96
97 public static ModelContextFactory newInstance( final String factoryClassName ) throws ModelContextFactoryError
98 {
99 if ( factoryClassName == null )
100 {
101 throw new NullPointerException( "factoryClassName" );
102 }
103
104 try
105 {
106 final Class<?> factoryClass = Class.forName( factoryClassName );
107
108 if ( !ModelContextFactory.class.isAssignableFrom( factoryClass ) )
109 {
110 throw new ModelContextFactoryError( getMessage( "illegalFactory", factoryClassName,
111 ModelContextFactory.class.getName() ) );
112
113 }
114
115 return factoryClass.asSubclass( ModelContextFactory.class ).newInstance();
116 }
117 catch ( final ClassNotFoundException e )
118 {
119 throw new ModelContextFactoryError( getMessage( "classNotFound", factoryClassName ), e );
120 }
121 catch ( final InstantiationException e )
122 {
123 final String message = getMessage( e );
124 throw new ModelContextFactoryError( getMessage( "instantiationException", factoryClassName,
125 message != null ? " " + message : "" ), e );
126
127 }
128 catch ( final IllegalAccessException e )
129 {
130 final String message = getMessage( e );
131 throw new ModelContextFactoryError( getMessage( "accessDenied", factoryClassName,
132 message != null ? " " + message : "" ), e );
133
134 }
135 }
136
137
138
139
140
141
142 public abstract ModelContext newModelContext();
143
144
145
146
147
148
149
150
151
152 public abstract ModelContext newModelContext( final ClassLoader classLoader );
153
154 private static String getMessage( final String key, final Object... args )
155 {
156 return MessageFormat.format( ResourceBundle.getBundle( ModelContextFactory.class.getName().replace( '.', '/' ),
157 Locale.getDefault() ).getString( key ), args );
158
159 }
160
161 private static String getMessage( final Throwable t )
162 {
163 return t != null
164 ? t.getMessage() != null && t.getMessage().trim().length() > 0
165 ? t.getMessage()
166 : getMessage( t.getCause() )
167 : null;
168
169 }
170
171 }