View Javadoc

1   /*
2    * Copyright 2004-2006 the Seasar Foundation and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not 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, 
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package org.seasar.tuigwaa.plugin;
17  
18  import java.util.HashMap;
19  import java.util.Map;
20  
21  import org.apache.axis.components.logger.LogFactory;
22  import org.apache.commons.logging.Log;
23  import org.seasar.framework.container.S2Container;
24  import org.seasar.framework.container.factory.SingletonS2ContainerFactory;
25  import org.seasar.tuigwaa.cms.core.wiki.base.WikiConfigurationFactory;
26  import org.seasar.tuigwaa.cms.core.wiki.base.WikiException;
27  import org.seasar.tuigwaa.controller.ControllerService;
28  import org.seasar.tuigwaa.model.DAOService;
29  import org.seasar.tuigwaa.model.ModelService;
30  import org.seasar.tuigwaa.util.TgwContext;
31  import org.seasar.tuigwaa.util.TgwPathResolver;
32  
33  import com.isenshi.util.ResourceUtils;
34  import com.isenshi.util.extlib.DiconResource;
35  
36  public class PluginServiceImpl implements PluginService {
37  
38  	private static final String ATTR_NAME = "org.seasar.tuigwaa.plugin.PuginService/MESSAGE:";
39  
40  	private Log log = LogFactory.getLog(getClass().getName());
41  
42  	private ControllerService controller;
43  
44  	private ModelService model;
45  
46  	private DAOService daoManager;
47  
48  	private S2Container container = SingletonS2ContainerFactory.getContainer()
49  			.getDescendant("dicon/plugin.dicon");
50  
51  	private Map extContainerMap = new HashMap();
52  
53  	private Map appliMap = new HashMap();
54  
55  	public PluginServiceImpl(ControllerService controller, ModelService model,
56  			DAOService daoservice) {
57  		this.controller = controller;
58  		this.model = model;
59  		this.daoManager = daoservice;
60  	}
61  
62  	public void sendMessageByAction(Class action, String message) {
63  		WebAppli appli = (WebAppli) appliMap.get(action);
64  		TgwContext.setRequestAttribute(ATTR_NAME + ":"
65  				+ appli.getViewClass().getName(), message);
66  	}
67  
68  	public String getMessage(Class viewClass) {
69  		return (String) TgwContext.getRequestAttribute(ATTR_NAME + ":"
70  				+ viewClass.getName());
71  	}
72  
73  	public Plugin getExtPlugin(String siteName, String pluginName) {
74  		return (Plugin) ext(siteName).getComponent(pluginName);
75  	}
76  
77  	public void loadPlugins() {
78  		initActions(null, container);
79  	}
80  
81  	public void loadExtPlugins(String siteName) {
82  		String extPlugins = TgwPathResolver.getExternalPluginDicon(siteName);
83  		if (ResourceUtils.isExist(extPlugins)) {
84  			DiconResource resource = new DiconResource();
85  			resource.setNamespace("sites." + siteName + ".plugin");
86  			resource.setPath(extPlugins);
87  			resource.load();
88  
89  			resource.bindRoot();
90  			resource.include("dicon/service.dicon");
91  
92  			S2Container container = resource.getContainer();
93  			extContainerMap.put(siteName, container);
94  
95  			initActions(siteName, container);
96  		}
97  	}
98  
99  	// [Start] ------ Private Method -------
100 
101 	private S2Container ext(String siteName) {
102 		return (S2Container) extContainerMap.get(siteName);
103 	}
104 
105 	private void initActions(String siteName, S2Container container) {
106 		Object[] actions = container.findComponents(ActionPlugin.class);
107 		if (actions == null) {
108 			return;
109 		}
110 		for (int i = 0; i < actions.length; i++) {
111 			initAction(siteName, (ActionPlugin)actions[i]);
112 		}
113 	}
114 	
115 	private void initAction(String siteName, ActionPlugin action) {
116 		try {
117 			WebAppli appli = new WebAppli(model, daoManager, controller);
118 			appli.autoRegisterByAction(action);
119 			if (siteName != null) {
120 				appli.setPrefix(siteName);
121 				Plugin view = (Plugin) ext(siteName).getComponent(
122 						appli.getViewClass());
123 				appli.setView(view);
124 				controller.addPluginConfig(siteName, appli);
125 			} else {
126 				Plugin view = WikiConfigurationFactory.getInstance()
127 						.createConfiguration().getPluginByClassname(
128 								appli.getViewClass().getName());
129 				appli.setView(view);
130 				log.info("Loaded WebAppli ..." + appli.getPath());
131 				controller.addPrototypeWebAppli(appli);
132 			}
133 			appliMap.put(action.getClass(), appli);
134 		} catch (WikiException e) {
135 			e.printStackTrace();
136 		} catch (SecurityException e) {
137 			log.warn("Can't regist plugin action caused by" + e.getMessage());
138 		} catch (ClassNotFoundException e) {
139 			log.warn("Can't regist plugin action caused by" + e.getMessage());
140 		} catch (NoSuchMethodException e) {
141 			log.warn("Can't regist plugin action caused by" + e.getMessage());
142 		}
143 	}
144 	
145 	
146 	
147 }