001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. The ASF licenses this file to You
004: * under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License. For additional information regarding
015: * copyright in this work, please see the NOTICE file in the top level
016: * directory of this distribution.
017: */
018:
019: package org.apache.roller.ui.core.plugins;
020:
021: import java.util.ArrayList;
022: import java.util.LinkedHashMap;
023: import java.util.List;
024: import java.util.Map;
025: import org.apache.commons.logging.Log;
026: import org.apache.commons.logging.LogFactory;
027: import org.apache.roller.config.RollerConfig;
028: import org.apache.commons.lang.StringUtils;
029:
030: /**
031: * Plugin management for UI layer plugins.
032: */
033: public class UIPluginManagerImpl implements UIPluginManager {
034:
035: private static Log log = LogFactory
036: .getLog(UIPluginManagerImpl.class);
037:
038: // singleton instance
039: private static UIPluginManagerImpl instance = null;
040:
041: // list of configured WeblogEntryEditor classes
042: Map editors = new LinkedHashMap();
043:
044: // the default WeblogEntryEditor
045: WeblogEntryEditor defaultEditor = null;
046:
047: static {
048: instance = new UIPluginManagerImpl();
049: }
050:
051: // private to enforce singleton pattern
052: private UIPluginManagerImpl() {
053: loadEntryEditorClasses();
054: }
055:
056: // static singleton accessor
057: public static UIPluginManager getInstance() {
058: return instance;
059: }
060:
061: public List getWeblogEntryEditors() {
062: // TODO: sort list of returned editors
063: return new ArrayList(this .editors.values());
064: }
065:
066: public WeblogEntryEditor getWeblogEntryEditor(String id) {
067:
068: WeblogEntryEditor editor = null;
069:
070: // see if this editor is configured
071: editor = (id == null) ? null : (WeblogEntryEditor) this .editors
072: .get(id);
073: if (editor == null) {
074: editor = this .defaultEditor;
075: }
076:
077: return editor;
078: }
079:
080: /**
081: * Initialize the set of configured editors and define the default editor.
082: */
083: private void loadEntryEditorClasses() {
084:
085: log.debug("Initializing entry editor plugins");
086:
087: String editorStr = RollerConfig
088: .getProperty("plugins.weblogEntryEditors");
089: if (editorStr != null) {
090:
091: String[] editorList = StringUtils.stripAll(StringUtils
092: .split(editorStr, ","));
093: for (int i = 0; i < editorList.length; i++) {
094:
095: log.debug("trying editor " + editorList[i]);
096:
097: try {
098: Class editorClass = Class.forName(editorList[i]);
099: WeblogEntryEditor editor = (WeblogEntryEditor) editorClass
100: .newInstance();
101:
102: // looks okay, add it to the map
103: this .editors.put(editor.getId(), editor);
104:
105: } catch (ClassCastException cce) {
106: log
107: .error(
108: "It appears that your editor does not implement "
109: + "the WeblogEntryEditor interface",
110: cce);
111: } catch (Exception e) {
112: log.error("Unable to instantiate editor ["
113: + editorList[i] + "]", e);
114: }
115: }
116: }
117:
118: if (this .editors.size() < 1) {
119: log
120: .warn("No entry editors configured, this means that publishing "
121: + "entries will be impossible.");
122: return;
123: }
124:
125: // make sure the default editor is defined
126: String defaultEditorId = RollerConfig
127: .getProperty("plugins.defaultEditor");
128: if (defaultEditorId != null) {
129: this .defaultEditor = (WeblogEntryEditor) this .editors
130: .get(defaultEditorId);
131: }
132:
133: if (this .defaultEditor == null) {
134: // someone didn't configure the default editor properly
135: // guess we'll just have to pick one for them
136: log
137: .warn("Default editor was not properly configured, picking one at random instead.");
138:
139: Object editor = this .editors.values().iterator().next();
140: this .defaultEditor = (WeblogEntryEditor) editor;
141: }
142: }
143:
144: }
|