KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > portlet > exomvc > config > Configuration


1 /***************************************************************************
2  * Copyright 2001-2003 The eXo Platform SARL All rights reserved. *
3  * Please look at license.txt in info directory for more license detail. *
4  **************************************************************************/

5 package org.exoplatform.portlet.exomvc.config;
6
7 import java.util.* ;
8 import javax.portlet.PortletConfig;
9 import javax.portlet.PortletContext;
10 import javax.portlet.PortletException;
11 import javax.portlet.PortletMode;
12 import org.exoplatform.portlet.exomvc.MVCPortletFramework;
13 /**
14  * @author Tuan Nguyen (tuan08@users.sourceforge.net)
15  * @since Nov 7, 2004
16  * @version $Id$
17  */

18 public class Configuration {
19   private Map viewMode_ ;
20   private Map editMode_ ;
21   private Map helpMode_ ;
22   private Map configMode_ ;
23   
24   private PageConfig defaultView_ ;
25   private PageConfig defaultEdit_ ;
26   private PageConfig defaultConfig_ ;
27   private PageConfig defaultHelp_ ;
28   
29   private PortletContext context_ ;
30   private PortletConfig portletConfig_ ;
31   private GroovyResourceManager groovyResourceManager_ ;
32   private JSPResourceManager jspResourceManager_ ;
33   private VelocityResourceManager velocityResourceManager_ ;
34   private String JavaDoc reposistory_ ;
35   
36   public Configuration(PortletConfig config) throws Exception JavaDoc {
37     context_ = config.getPortletContext() ;
38     portletConfig_ = config ;
39     configure() ;
40   }
41   
42   public void configure() throws Exception JavaDoc {
43     defaultView_ = null;
44     defaultEdit_ = null ;
45     defaultConfig_ = null ;
46     defaultHelp_ = null ;
47     KeyComparator comparator = new KeyComparator() ;
48     viewMode_ = new TreeMap(comparator) ;
49     editMode_ = new TreeMap(comparator) ;
50     helpMode_ = new TreeMap(comparator) ;
51     configMode_ = new TreeMap(comparator) ;
52     
53     reposistory_ = portletConfig_.getInitParameter("script.reposistory") ;
54     if(reposistory_ == null) reposistory_ = "/WEB-INF/script" ;
55     String JavaDoc configScript = portletConfig_.getInitParameter("configure.script") ;
56     jspResourceManager_ = new JSPResourceManager(context_, reposistory_) ;
57     groovyResourceManager_ = new GroovyResourceManager(context_, reposistory_) ;
58     Configure configure =
59       (Configure)groovyResourceManager_.getGroovyManager().getObject(configScript) ;
60     configure.configure(this) ;
61   }
62   
63   public PageConfig getPageConfig(PortletMode mode, String JavaDoc pageName) throws Exception JavaDoc {
64     PageConfig pconfig = null ;
65     if (mode.equals(PortletMode.VIEW)) {
66       if(pageName == null) pconfig = defaultView_ ;
67       else pconfig = (PageConfig)viewMode_.get(pageName) ;
68     } else if (mode.equals(PortletMode.EDIT)) {
69       if(pageName == null) pconfig = defaultEdit_ ;
70       else pconfig = (PageConfig)editMode_.get(pageName) ;
71     } else if (mode.equals(MVCPortletFramework.CONFIG_MODE)) {
72       if(pageName == null) pconfig = defaultConfig_ ;
73       else pconfig = (PageConfig)configMode_.get(pageName) ;
74     } else if (mode.equals(PortletMode.HELP)) {
75       if(pageName == null) pconfig = defaultHelp_ ;
76       else pconfig = (PageConfig)helpMode_.get(pageName) ;
77     } else {
78       throw new PortletException("unknown portlet mode: " + mode);
79     }
80     return pconfig ;
81   }
82   
83   public PageConfig getDefaultView() { return defaultView_ ; }
84   
85   public void addViewModePage(PageConfig pconfig) {
86     viewMode_.put(pconfig.getPageName(), pconfig) ;
87     if(defaultView_ == null) defaultView_ = pconfig ;
88   }
89   
90   public void addEditModePage(PageConfig pconfig) {
91     editMode_.put(pconfig.getPageName(), pconfig) ;
92     if(defaultEdit_ == null) defaultEdit_ = pconfig ;
93   }
94   
95   public void addHelpModePage(PageConfig pconfig) {
96     helpMode_.put(pconfig.getPageName(), pconfig) ;
97     if(defaultHelp_ == null) defaultHelp_ = pconfig ;
98   }
99   
100   public void addConfigModePage(PageConfig pconfig) {
101     configMode_.put(pconfig.getPageName(), pconfig) ;
102     if(defaultConfig_ == null) defaultConfig_ = pconfig ;
103   }
104   
105   public Map getViewModePages() { return viewMode_ ; }
106   
107   public PortletConfig getPortletConfig() { return portletConfig_ ; }
108   public PortletContext getPortletContext() { return context_ ;}
109   
110   public GroovyResourceManager getGroovyResourceManager() { return groovyResourceManager_ ; }
111   
112   public JSPResourceManager getJSPResourceManager() { return jspResourceManager_ ; }
113   
114   public VelocityResourceManager getVelocityResourceManager() throws Exception JavaDoc {
115     if(velocityResourceManager_ == null) {
116       synchronized(this) {
117         velocityResourceManager_ = new VelocityResourceManager(context_, reposistory_) ;
118       }
119     }
120     return velocityResourceManager_ ;
121   }
122   
123   public void destroy() {
124     groovyResourceManager_.destroy() ;
125   }
126   
127   static class KeyComparator implements Comparator {
128     public int compare(Object JavaDoc o1, Object JavaDoc o2) {
129       String JavaDoc key1 = (String JavaDoc) o1 ;
130       String JavaDoc key2 = (String JavaDoc) o2 ;
131       return key1.compareTo(key2) ;
132      }
133   }
134 }
Popular Tags