001: /***********************************************************************************
002: *
003: * Copyright (c) 2003, 2004, 2005, 2006 The Sakai Foundation.
004: *
005: * Licensed under the Educational Community License, Version 1.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.opensource.org/licenses/ecl1.php
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: **********************************************************************************/package org.sakaiproject.archive.impl;
018:
019: import java.io.File;
020: import java.util.Collections;
021: import java.util.Iterator;
022: import java.util.List;
023: import java.util.Stack;
024: import java.util.Vector;
025:
026: import org.apache.commons.logging.Log;
027: import org.apache.commons.logging.LogFactory;
028: import org.sakaiproject.authz.api.AuthzGroup;
029: import org.sakaiproject.authz.api.GroupNotDefinedException;
030: import org.sakaiproject.authz.api.Role; //import org.sakaiproject.authz.cover.AuthzGroupService;
031: import org.sakaiproject.authz.api.AuthzGroupService;
032: import org.sakaiproject.component.api.ServerConfigurationService;
033: import org.sakaiproject.entity.api.EntityManager;
034: import org.sakaiproject.entity.api.EntityProducer;
035: import org.sakaiproject.exception.IdUnusedException;
036: import org.sakaiproject.site.api.Site;
037: import org.sakaiproject.site.api.SiteService; //import org.sakaiproject.site.cover.SiteService;
038: import org.sakaiproject.time.api.Time; //import org.sakaiproject.time.cover.TimeService;
039: import org.sakaiproject.time.api.TimeService;
040: import org.sakaiproject.user.api.User;
041: import org.sakaiproject.user.api.UserDirectoryService;
042: import org.sakaiproject.content.api.ContentHostingService; //import org.sakaiproject.user.cover.UserDirectoryService;
043: import org.sakaiproject.util.Xml;
044: import org.w3c.dom.Document;
045: import org.w3c.dom.Element;
046:
047: public class SiteArchiver {
048:
049: private static Log M_log = LogFactory.getLog(SiteArchiver.class);
050:
051: /** Dependency: ServerConfigurationService. */
052: protected ServerConfigurationService m_serverConfigurationService = null;
053:
054: public void setServerConfigurationService(
055: ServerConfigurationService service) {
056: m_serverConfigurationService = service;
057: }
058:
059: /** Dependency: EntityManager. */
060: protected EntityManager m_entityManager = null;
061:
062: public void setEntityManager(EntityManager service) {
063: m_entityManager = service;
064: }
065:
066: /** Dependency: SiteService */
067: protected SiteService m_siteService = null;
068:
069: public void setSiteService(SiteService service) {
070: m_siteService = service;
071: }
072:
073: /** Dependency: AuthzService */
074: protected AuthzGroupService m_authzGroupService = null;
075:
076: public void setAuthzGroupService(AuthzGroupService service) {
077: m_authzGroupService = service;
078: }
079:
080: /** Dependency: UserDirectoryService */
081: protected UserDirectoryService m_userDirectoryService = null;
082:
083: public void setUserDirectoryService(UserDirectoryService service) {
084: m_userDirectoryService = service;
085: }
086:
087: /** Dependency: TimeService */
088: protected TimeService m_timeService = null;
089:
090: public void setTimeService(TimeService service) {
091: m_timeService = service;
092: }
093:
094: /** Dependency: ContentHosting */
095: protected ContentHostingService m_contentHostingService = null;
096:
097: public void setContentHostingService(ContentHostingService service) {
098: m_contentHostingService = service;
099: }
100:
101: public String archive(String siteId, String m_storagePath,
102: String fromSystem) {
103: StringBuffer results = new StringBuffer();
104:
105: if (M_log.isDebugEnabled())
106: M_log.debug("archive(): site: " + siteId);
107:
108: Site theSite = null;
109: try {
110: theSite = m_siteService.getSite(siteId);
111: } catch (IdUnusedException e) {
112: results.append("Site: " + siteId + " not found.\n");
113: M_log.warn("archive(): site not found: " + siteId);
114: return results.toString();
115: }
116:
117: // collect all the attachments we need
118: List attachments = m_entityManager.newReferenceList();
119:
120: Time now = m_timeService.newTime();
121:
122: // this is the folder we are writing files to
123: String storagePath = m_storagePath + siteId + "-archive/";
124:
125: // create the directory for the archive
126: File dir = new File(m_storagePath + siteId + "-archive/");
127: dir.mkdirs();
128:
129: // for each registered ResourceService, give it a chance to archve
130: List services = m_entityManager.getEntityProducers();
131: for (Iterator iServices = services.iterator(); iServices
132: .hasNext();) {
133: EntityProducer service = (EntityProducer) iServices.next();
134: if (service == null)
135: continue;
136: if (!service.willArchiveMerge())
137: continue;
138:
139: Document doc = Xml.createDocument();
140: Stack stack = new Stack();
141: Element root = doc.createElement("archive");
142: doc.appendChild(root);
143: root.setAttribute("source", siteId);
144: root.setAttribute("server", m_serverConfigurationService
145: .getServerId());
146: root.setAttribute("date", now.toString());
147: root.setAttribute("system", fromSystem);
148:
149: stack.push(root);
150:
151: try {
152: String msg = service.archive(siteId, doc, stack,
153: storagePath, attachments);
154: results.append(msg);
155: } catch (Throwable t) {
156: results.append(t.toString() + "\n");
157: }
158:
159: stack.pop();
160:
161: String fileName = storagePath + service.getLabel() + ".xml";
162: Xml.writeDocument(doc, fileName);
163: }
164:
165: // archive the collected attachments
166: if (attachments.size() > 0) {
167: Document doc = Xml.createDocument();
168: Stack stack = new Stack();
169: Element root = doc.createElement("archive");
170: doc.appendChild(root);
171: root.setAttribute("source", siteId);
172: root.setAttribute("server", m_serverConfigurationService
173: .getServerId());
174: root.setAttribute("date", now.toString());
175: root.setAttribute("system", fromSystem);
176:
177: stack.push(root);
178:
179: //SWG String msg = org.sakaiproject.content.cover.ContentHostingService.archiveResources(attachments, doc, stack, storagePath);
180: String msg = m_contentHostingService.archiveResources(
181: attachments, doc, stack, storagePath);
182: results.append(msg);
183:
184: stack.pop();
185:
186: String fileName = storagePath + "attachment.xml";
187: Xml.writeDocument(doc, fileName);
188: }
189:
190: // *** Site
191:
192: Document doc = Xml.createDocument();
193: Stack stack = new Stack();
194: Element root = doc.createElement("archive");
195: doc.appendChild(root);
196: root.setAttribute("site", siteId);
197: root.setAttribute("date", now.toString());
198: root.setAttribute("system", fromSystem);
199:
200: stack.push(root);
201:
202: String msg = archiveSite(theSite, doc, stack);
203: results.append(msg);
204:
205: stack.pop();
206: Xml.writeDocument(doc, m_storagePath + siteId
207: + "-archive/site.xml");
208:
209: // *** Users
210: doc = Xml.createDocument();
211: stack = new Stack();
212: root = doc.createElement("archive");
213: doc.appendChild(root);
214: root.setAttribute("site", siteId);
215: root.setAttribute("date", now.toString());
216: root.setAttribute("system", fromSystem);
217:
218: stack.push(root);
219:
220: msg = archiveUsers(theSite, doc, stack);
221: results.append(msg);
222:
223: stack.pop();
224: Xml.writeDocument(doc, m_storagePath + siteId
225: + "-archive/user.xml");
226:
227: return results.toString();
228:
229: } // archive
230:
231: /**
232: * Archive the site definition.
233: * @param site the site.
234: * @param doc The document to contain the xml.
235: * @param stack The stack of elements, the top of which will be the containing
236: * element of the "site" element.
237: */
238:
239: protected String archiveSite(Site site, Document doc, Stack stack) {
240: Element element = doc.createElement(SiteService.APPLICATION_ID);
241: ((Element) stack.peek()).appendChild(element);
242: stack.push(element);
243:
244: Element siteNode = site.toXml(doc, stack);
245: stack.push(siteNode);
246:
247: // to add the realm node with user list into site
248: List roles = new Vector();
249: String realmId = m_siteService.siteReference(site.getId()); //SWG "/site/" + site.getId();
250: try {
251: Role role = null;
252: AuthzGroup realm = m_authzGroupService
253: .getAuthzGroup(realmId);
254:
255: Element realmNode = doc.createElement("roles");
256: ((Element) stack.peek()).appendChild(realmNode);
257: stack.push(realmNode);
258:
259: roles.addAll(realm.getRoles());
260:
261: for (int i = 0; i < roles.size(); i++) {
262: role = (Role) roles.get(i);
263: String roleId = role.getId();
264: Element node = doc.createElement(roleId);
265: realmNode.appendChild(node);
266:
267: List users = new Vector();
268: users.addAll(realm.getUsersHasRole(role.getId()));
269: for (int j = 0; j < users.size(); j++) {
270: Element abilityNode = doc.createElement("ability");
271: abilityNode.setAttribute("roleId", roleId);
272: abilityNode.setAttribute("userId", ((String) users
273: .get(j)));
274: node.appendChild(abilityNode);
275: }
276: }
277: } catch (Exception any) {
278: M_log.warn("archve: exception archiving site: "
279: + site.getId() + ": ", any);
280: }
281:
282: stack.pop();
283:
284: return "archiving Site: " + site.getId() + "\n";
285:
286: } // archiveSite
287:
288: /**
289: * Archive the users defined in this site (internal users only).
290: * @param site the site.
291: * @param doc The document to contain the xml.
292: * @param stack The stack of elements, the top of which will be the containing
293: * element of the "site" element.
294: */
295: protected String archiveUsers(Site site, Document doc, Stack stack) {
296: Element element = doc
297: .createElement(UserDirectoryService.APPLICATION_ID);
298: ((Element) stack.peek()).appendChild(element);
299: stack.push(element);
300:
301: try {
302: // get the site's user list
303: List users = new Vector();
304: String realmId = m_siteService.siteReference(site.getId()); //SWG "/site/" + site.getId();
305: try {
306: AuthzGroup realm = m_authzGroupService
307: .getAuthzGroup(realmId);
308: users.addAll(m_userDirectoryService.getUsers(realm
309: .getUsers()));
310: Collections.sort(users);
311: for (int i = 0; i < users.size(); i++) {
312: User user = (User) users.get(i);
313: user.toXml(doc, stack);
314: }
315: } catch (GroupNotDefinedException e) {
316: M_log.warn(e, e);
317: } catch (Exception any) {
318: M_log.warn(any, any);
319: }
320:
321: } catch (Exception any) {
322: M_log.warn(any, any);
323: }
324:
325: stack.pop();
326:
327: return "archiving the users for Site: " + site.getId() + "\n";
328:
329: } // archiveUsers
330: }
|