File Source: UIPluginManagerImpl.java
1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. The ASF licenses this file to You
4 * under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License. For additional information regarding
15 * copyright in this work, please see the NOTICE file in the top level
16 * directory of this distribution.
17 */
18
19 package org.apache.roller.weblogger.ui.core.plugins;
20
21 import java.util.ArrayList;
22 import java.util.LinkedHashMap;
23 import java.util.List;
24 import java.util.Map;
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27 import org.apache.roller.weblogger.config.WebloggerConfig;
28 import org.apache.commons.lang.StringUtils;
29
30
31 /**
32 * Plugin management for UI layer plugins.
33 */
34 public class UIPluginManagerImpl implements UIPluginManager {
35
/*
P/P * Method: org.apache.roller.weblogger.ui.core.plugins.UIPluginManagerImpl__static_init
*
* Presumptions:
* org.apache.commons.logging.LogFactory:getLog(...)@36 != null
*
* Postconditions:
* instance == &new UIPluginManagerImpl(UIPluginManagerImpl__static_init#1)
* (soft) log != null
* new LinkedHashMap(UIPluginManagerImpl#1) num objects == 1
* new UIPluginManagerImpl(UIPluginManagerImpl__static_init#1) num objects == 1
* init'ed(instance.defaultEditor)
* instance.editors == &new LinkedHashMap(UIPluginManagerImpl#1)
*/
36 private static Log log = LogFactory.getLog(UIPluginManagerImpl.class);
37
38 // singleton instance
39 private static UIPluginManagerImpl instance = null;
40
41 // list of configured WeblogEntryEditor classes
42 Map editors = new LinkedHashMap();
43
44 // the default WeblogEntryEditor
45 WeblogEntryEditor defaultEditor = null;
46
47
48 static {
49 instance = new UIPluginManagerImpl();
50 }
51
52
53 // private to enforce singleton pattern
/*
P/P * Method: void org.apache.roller.weblogger.ui.core.plugins.UIPluginManagerImpl()
*
* Preconditions:
* log != null
*
* Postconditions:
* init'ed(this.defaultEditor)
* this.editors == &new LinkedHashMap(UIPluginManagerImpl#1)
* new LinkedHashMap(UIPluginManagerImpl#1) num objects == 1
*/
54 private UIPluginManagerImpl() {
55 loadEntryEditorClasses();
56 }
57
58
59 // static singleton accessor
60 public static UIPluginManager getInstance() {
/*
P/P * Method: UIPluginManager getInstance()
*
* Preconditions:
* init'ed(instance)
*
* Postconditions:
* return_value == instance
* init'ed(return_value)
*/
61 return instance;
62 }
63
64
65 public List getWeblogEntryEditors() {
66 // TODO: sort list of returned editors
/*
P/P * Method: List getWeblogEntryEditors()
*
* Preconditions:
* this.editors != null
*
* Postconditions:
* return_value == &new ArrayList(getWeblogEntryEditors#1)
* new ArrayList(getWeblogEntryEditors#1) num objects == 1
*/
67 return new ArrayList(this.editors.values());
68 }
69
70
71 public WeblogEntryEditor getWeblogEntryEditor(String id) {
72
/*
P/P * Method: WeblogEntryEditor getWeblogEntryEditor(String)
*
* Preconditions:
* (soft) init'ed(this.defaultEditor)
* (soft) this.editors != null
*
* Postconditions:
* init'ed(return_value)
*/
73 WeblogEntryEditor editor = null;
74
75 // see if this editor is configured
76 editor = (id == null) ? null : (WeblogEntryEditor) this.editors.get(id);
77 if(editor == null) {
78 editor = this.defaultEditor;
79 }
80
81 return editor;
82 }
83
84
85 /**
86 * Initialize the set of configured editors and define the default editor.
87 */
88 private void loadEntryEditorClasses() {
89
/*
P/P * Method: void loadEntryEditorClasses()
*
* Preconditions:
* log != null
* this.editors != null
* (soft) init'ed(this.defaultEditor)
*
* Presumptions:
* editorList.length@95 <= 232-1
* java.lang.Class:forName(...)@101 != null
* java.util.Map:values(...)@133 != null
* org.apache.commons.lang.StringUtils:stripAll(...)@95 != null
*
* Postconditions:
* init'ed(this.defaultEditor)
*
* Test Vectors:
* java.util.Map:size(...)@116: {1..232-1}, {-231..0}
* org.apache.roller.weblogger.config.WebloggerConfig:getProperty(...)@123: Addr_Set{null}, Inverse{null}
* org.apache.roller.weblogger.config.WebloggerConfig:getProperty(...)@92: Addr_Set{null}, Inverse{null}
*/
90 log.debug("Initializing entry editor plugins");
91
92 String editorStr = WebloggerConfig.getProperty("plugins.weblogEntryEditors");
93 if (editorStr != null) {
94
95 String[] editorList = StringUtils.stripAll(StringUtils.split(editorStr, ","));
96 for (int i=0; i < editorList.length; i++) {
97
98 log.debug("trying editor " + editorList[i]);
99
100 try {
101 Class editorClass = Class.forName(editorList[i]);
102 WeblogEntryEditor editor = (WeblogEntryEditor) editorClass.newInstance();
103
104 // looks okay, add it to the map
105 this.editors.put(editor.getId(), editor);
106
107 } catch(ClassCastException cce) {
108 log.error("It appears that your editor does not implement "+
109 "the WeblogEntryEditor interface", cce);
110 } catch(Exception e) {
111 log.error("Unable to instantiate editor ["+editorList[i]+"]", e);
112 }
113 }
114 }
115
116 if(this.editors.size() < 1) {
117 log.warn("No entry editors configured, this means that publishing "+
118 "entries will be impossible.");
119 return;
120 }
121
122 // make sure the default editor is defined
123 String defaultEditorId = WebloggerConfig.getProperty("plugins.defaultEditor");
124 if(defaultEditorId != null) {
125 this.defaultEditor = (WeblogEntryEditor) this.editors.get(defaultEditorId);
126 }
127
128 if(this.defaultEditor == null) {
129 // someone didn't configure the default editor properly
130 // guess we'll just have to pick one for them
131 log.warn("Default editor was not properly configured, picking one at random instead.");
132
133 Object editor = this.editors.values().iterator().next();
134 this.defaultEditor = (WeblogEntryEditor) editor;
135 }
136 }
137
138 }
SofCheck Inspector Build Version : 2.18479
| UIPluginManagerImpl.java |
2009-Jan-02 14:25:48 |
| UIPluginManagerImpl.class |
2009-Sep-04 03:12:44 |