001: package ru.emdev.EmForge.wiki;
002:
003: import java.io.File;
004: import java.io.IOException;
005: import java.io.InputStream;
006: import java.io.InputStreamReader;
007: import java.security.Security;
008: import java.util.Properties;
009:
010: import org.apache.commons.logging.Log;
011: import org.apache.commons.logging.LogFactory;
012: import org.jbpm.util.ClassLoaderUtil;
013: import org.springframework.beans.factory.FactoryBean;
014: import org.springframework.beans.factory.InitializingBean;
015: import org.springframework.context.ApplicationContext;
016: import org.springframework.context.ApplicationContextAware;
017: import org.springframework.core.io.Resource;
018: import org.springframework.transaction.PlatformTransactionManager;
019: import org.springframework.transaction.TransactionStatus;
020: import org.springframework.transaction.support.TransactionCallback;
021: import org.springframework.transaction.support.TransactionTemplate;
022: import org.springframework.util.FileCopyUtils;
023:
024: import com.ecyrd.jspwiki.WikiContext;
025: import com.ecyrd.jspwiki.WikiEngine;
026: import com.ecyrd.jspwiki.WikiException;
027: import com.ecyrd.jspwiki.WikiPage;
028:
029: /** This is Bean for making JspWiki WikiEngine via Spring Configuration
030: *
031: * @author akakunin
032: *
033: */
034: public class JspWikiEngineBean implements FactoryBean,
035: InitializingBean, ApplicationContextAware {
036: protected final Log logger = LogFactory.getLog(getClass());
037: public static final String SPRING_CONTEXT_PROPERTY = "jspwiki.spring.context";
038:
039: private WikiEngine m_engine;
040:
041: /// properties
042: private Properties m_props;
043:
044: private Resource m_initWikiPagesFolder;
045: private TransactionTemplate transactionTemplate;
046:
047: ApplicationContext m_appContext;
048:
049: public void setApplicationContext(ApplicationContext i_appContext) {
050: m_appContext = i_appContext;
051: }
052:
053: /// setter for properties
054: public void setWikiProperties(Properties i_props) {
055: m_props = i_props;
056: }
057:
058: public void setInitWikiPagesFolder(Resource i_initWikiPagesFolder) {
059: m_initWikiPagesFolder = i_initWikiPagesFolder;
060: }
061:
062: /// Created WikiEngine
063: //private WikiEngine m_engine;
064:
065: public void setTxManager(PlatformTransactionManager i_txManager) {
066: transactionTemplate = new TransactionTemplate(i_txManager);
067: }
068:
069: /** Gets the WikiEngine
070: *
071: * @return WikiEngine
072: */
073: public WikiEngine getEngine() throws Exception {
074: return (WikiEngine) getObject();
075: }
076:
077: public Object getObject() throws Exception {
078: if (m_engine == null) {
079: m_engine = createEngine();
080: }
081:
082: assert m_engine != null;
083: return m_engine;
084: }
085:
086: public Class<?> getObjectType() {
087: return WikiEngine.class;
088: }
089:
090: public boolean isSingleton() {
091: return true;
092: }
093:
094: public void afterPropertiesSet() throws Exception {
095: if (m_props == null) {
096: throw new IllegalArgumentException(
097: "wikiProperties should be specified for wikiEngine bean");
098: }
099:
100: //add spring context into properties to access it from bean wrappers
101: m_props.put(SPRING_CONTEXT_PROPERTY, m_appContext);
102: }
103:
104: /** Creates WikiEngine object
105: *
106: * @throws Exception
107: */
108: protected WikiEngine createEngine() throws Exception {
109: return (WikiEngine) transactionTemplate
110: .execute(new TransactionCallback() {
111:
112: // the code in this method executes in a transactional context
113: public Object doInTransaction(
114: TransactionStatus status) {
115: try {
116: assert m_props != null;
117: assert m_engine == null;
118:
119: Security
120: .setProperty("policy.provider",
121: "ru.emdev.EmForge.wiki.security.PolicyImpl");
122:
123: WikiEngine engine = new WikiEngine(m_props);
124: assert engine != null;
125:
126: initPages(engine);
127: return engine;
128: } catch (Exception ex) {
129: logger.error("Cannot create wiki engine",
130: ex);
131: status.setRollbackOnly();
132: return null;
133: }
134: }
135: });
136: }
137:
138: private void initPages(WikiEngine i_engine) {
139: assert m_initWikiPagesFolder != null;
140:
141: try {
142: File directory = m_initWikiPagesFolder.getFile();
143: String[] files = directory.list();
144:
145: for (int i = 0; i < files.length; i++) {
146: if (files[i].endsWith(".txt")) {
147: String pageName = files[i].substring(0, files[i]
148: .length() - 4);
149:
150: // check - is this name exists in wikiEngine
151: WikiPage page = i_engine.getPage(pageName);
152:
153: if (page == null) {
154: logger.info("Create " + pageName + " page");
155: // create EditPageHelp page if it is not exists
156: page = new WikiPage(i_engine, pageName);
157:
158: WikiContext wikiContext = new WikiContext(
159: i_engine, page);
160:
161: InputStream pageStream = ClassLoaderUtil
162: .getStream("ru/emdev/EmForge/wiki/pages/"
163: + files[i]);
164: String pageText = FileCopyUtils
165: .copyToString(new InputStreamReader(
166: pageStream));
167:
168: i_engine.saveText(wikiContext, pageText);
169:
170: }
171: }
172: }
173: } catch (IOException ioEx) {
174: logger.error("Cannot load wikiPages from resources", ioEx);
175: } catch (WikiException wikiEx) {
176: logger.error("Cannot store wikiPages ", wikiEx);
177: }
178:
179: }
180: }
|