1
22
23 package com.liferay.portal.deploy.hot;
24
25 import com.liferay.portal.kernel.deploy.hot.HotDeployEvent;
26 import com.liferay.portal.kernel.deploy.hot.HotDeployException;
27 import com.liferay.portal.kernel.deploy.hot.HotDeployListener;
28 import com.liferay.portal.kernel.util.ObjectValuePair;
29 import com.liferay.portal.service.impl.LayoutTemplateLocalUtil;
30 import com.liferay.util.CollectionFactory;
31 import com.liferay.util.Http;
32
33 import java.util.Iterator;
34 import java.util.List;
35 import java.util.Map;
36
37 import javax.servlet.ServletContext;
38
39 import org.apache.commons.logging.Log;
40 import org.apache.commons.logging.LogFactory;
41
42
51 public class LayoutTemplateHotDeployListener implements HotDeployListener {
52
53 public void invokeDeploy(HotDeployEvent event) throws HotDeployException {
54 String servletContextName = null;
55
56 try {
57 ServletContext ctx = event.getServletContext();
58
59 servletContextName = ctx.getServletContextName();
60
61 if (_log.isDebugEnabled()) {
62 _log.debug("Invoking deploy for " + servletContextName);
63 }
64
65 String[] xmls = new String[] {
66 Http.URLtoString(
67 ctx.getResource("/WEB-INF/liferay-layout-templates.xml"))
68 };
69
70 if (xmls[0] == null) {
71 return;
72 }
73
74 if (_log.isInfoEnabled()) {
75 _log.info(
76 "Registering layout templates for " + servletContextName);
77 }
78
79 List layoutTemplateIds = LayoutTemplateLocalUtil.init(
80 servletContextName, ctx, xmls, event.getPluginPackage());
81
82 _vars.put(servletContextName, layoutTemplateIds);
83
84 if (_log.isInfoEnabled()) {
85 _log.info(
86 "Layout templates for " + servletContextName +
87 " registered successfully");
88 }
89 }
90 catch (Exception e) {
91 throw new HotDeployException(
92 "Error registering layout templates for " + servletContextName,
93 e);
94 }
95 }
96
97 public void invokeUndeploy(HotDeployEvent event) throws HotDeployException {
98 String servletContextName = null;
99
100 try {
101 ServletContext ctx = event.getServletContext();
102
103 servletContextName = ctx.getServletContextName();
104
105 if (_log.isDebugEnabled()) {
106 _log.debug("Invoking undeploy for " + servletContextName);
107 }
108
109 List layoutTemplateIds = (List)_vars.get(servletContextName);
110
111 if (layoutTemplateIds != null) {
112 if (_log.isInfoEnabled()) {
113 _log.info(
114 "Unregistering layout templates for " +
115 servletContextName);
116 }
117
118 Iterator itr = layoutTemplateIds.iterator();
119
120 while (itr.hasNext()) {
121 ObjectValuePair ovp = (ObjectValuePair)itr.next();
122
123 String layoutTemplateId = (String)ovp.getKey();
124 Boolean standard = (Boolean)ovp.getValue();
125
126 try {
127 LayoutTemplateLocalUtil.uninstallLayoutTemplate(
128 layoutTemplateId, standard.booleanValue());
129 }
130 catch (Exception e1) {
131 _log.error(e1.getMessage());
132 }
133 }
134
135 layoutTemplateIds = null;
136
137 if (_log.isInfoEnabled()) {
138 _log.info(
139 "Layout templates for " + servletContextName +
140 " unregistered successfully");
141 }
142 }
143 }
144 catch (Exception e2) {
145 throw new HotDeployException(
146 "Error unregistering layout templates for " +
147 servletContextName,
148 e2);
149 }
150 }
151
152 private static Log _log =
153 LogFactory.getLog(LayoutTemplateHotDeployListener.class);
154
155 private static Map _vars = CollectionFactory.getHashMap();
156
157 }