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.HttpUtil;
29 import com.liferay.portal.kernel.util.ObjectValuePair;
30 import com.liferay.portal.service.impl.LayoutTemplateLocalUtil;
31
32 import java.util.HashMap;
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 HttpUtil.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<ObjectValuePair<String, Boolean>> layoutTemplateIds =
80 LayoutTemplateLocalUtil.init(
81 servletContextName, ctx, xmls, event.getPluginPackage());
82
83 _vars.put(servletContextName, layoutTemplateIds);
84
85 if (_log.isInfoEnabled()) {
86 _log.info(
87 "Layout templates for " + servletContextName +
88 " registered successfully");
89 }
90 }
91 catch (Exception e) {
92 throw new HotDeployException(
93 "Error registering layout templates for " + servletContextName,
94 e);
95 }
96 }
97
98 public void invokeUndeploy(HotDeployEvent event) throws HotDeployException {
99 String servletContextName = null;
100
101 try {
102 ServletContext ctx = event.getServletContext();
103
104 servletContextName = ctx.getServletContextName();
105
106 if (_log.isDebugEnabled()) {
107 _log.debug("Invoking undeploy for " + servletContextName);
108 }
109
110 List<ObjectValuePair<String, Boolean>> layoutTemplateIds =
111 _vars.get(servletContextName);
112
113 if (layoutTemplateIds != null) {
114 if (_log.isInfoEnabled()) {
115 _log.info(
116 "Unregistering layout templates for " +
117 servletContextName);
118 }
119
120 Iterator<ObjectValuePair<String, Boolean>> itr =
121 layoutTemplateIds.iterator();
122
123 while (itr.hasNext()) {
124 ObjectValuePair<String, Boolean> ovp = itr.next();
125
126 String layoutTemplateId = ovp.getKey();
127 Boolean standard = ovp.getValue();
128
129 try {
130 LayoutTemplateLocalUtil.uninstallLayoutTemplate(
131 layoutTemplateId, standard.booleanValue());
132 }
133 catch (Exception e) {
134 _log.error(e.getMessage());
135 }
136 }
137
138 layoutTemplateIds = null;
139
140 if (_log.isInfoEnabled()) {
141 _log.info(
142 "Layout templates for " + servletContextName +
143 " unregistered successfully");
144 }
145 }
146 }
147 catch (Exception e) {
148 throw new HotDeployException(
149 "Error unregistering layout templates for " +
150 servletContextName,
151 e);
152 }
153 }
154
155 private static Log _log =
156 LogFactory.getLog(LayoutTemplateHotDeployListener.class);
157
158 private static Map<String, List<ObjectValuePair<String, Boolean>>> _vars =
159 new HashMap<String, List<ObjectValuePair<String, Boolean>>>();
160
161 }