001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
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.apache.jetspeed.serializer;
018:
019: import java.util.Collection;
020: import java.util.Iterator;
021: import java.util.Vector;
022: import java.util.prefs.Preferences;
023:
024: import javolution.xml.XMLBinding;
025:
026: import org.apache.jetspeed.components.ComponentManager;
027: import org.apache.jetspeed.components.portletentity.PortletEntityAccessComponent;
028: import org.apache.jetspeed.components.portletregistry.PortletRegistry;
029: import org.apache.jetspeed.om.common.portlet.MutablePortletApplication;
030: import org.apache.jetspeed.om.common.portlet.MutablePortletEntity;
031: import org.apache.jetspeed.om.preference.impl.PrefsPreference;
032: import org.apache.jetspeed.om.preference.impl.PrefsPreferenceSetImpl;
033: import org.apache.jetspeed.prefs.PreferencesProvider;
034: import org.apache.jetspeed.serializer.objects.JSApplication;
035: import org.apache.jetspeed.serializer.objects.JSApplications;
036: import org.apache.jetspeed.serializer.objects.JSEntities;
037: import org.apache.jetspeed.serializer.objects.JSEntity;
038: import org.apache.jetspeed.serializer.objects.JSEntityPreference;
039: import org.apache.jetspeed.serializer.objects.JSEntityPreferences;
040: import org.apache.jetspeed.serializer.objects.JSNVPElements;
041: import org.apache.jetspeed.serializer.objects.JSPortlet;
042: import org.apache.jetspeed.serializer.objects.JSPortlets;
043: import org.apache.jetspeed.serializer.objects.JSSecondaryData;
044: import org.apache.pluto.om.common.Preference;
045: import org.apache.pluto.om.portlet.PortletDefinition;
046: import org.apache.pluto.om.portlet.PortletDefinitionList;
047:
048: /**
049: * Jetspeed Serializer - Secondary Data
050: * <p>
051: * The Serializer is capable of reading and writing additional content of the
052: * Jetspeed environment such as entities and preferences to and from XML files.
053: * The component can be used from a standalone java application for seeding a
054: * new database or from a running portal as an administrative backup/restore
055: * function.
056: * <p>
057: *
058: * @author <a href="mailto:hajo@bluesunrise.com">Hajo Birthelmer</a>
059: * @version $Id: $
060: */
061: public class JetspeedSerializerSecondaryImpl extends
062: JetspeedSerializerBase implements JetspeedSerializer {
063:
064: boolean overwrite = true;
065: int refCouter = 0;
066:
067: private PortletEntityAccessComponent entityAccess = null;
068:
069: private PortletRegistry registry;
070:
071: private PreferencesProvider prefProvider;
072:
073: protected Class getSerializerDataClass() {
074: return JSSecondaryData.class;
075: }
076:
077: protected String getSerializerDataTag() {
078: return TAG_SECONDARYSNAPSHOT;
079: }
080:
081: public JetspeedSerializerSecondaryImpl() {
082: super ();
083: }
084:
085: /**
086: * hand over existing component manager
087: *
088: * @param cm
089: */
090: public JetspeedSerializerSecondaryImpl(ComponentManager cm) {
091: super (cm);
092: }
093:
094: /**
095: * This constructor takes the application root, the search path for the boot
096: * component configuration files and the search path for the application
097: * component configuration files.
098: * <p>
099: * For example: new JetspeedSerializerImpl("./", "assembly/boot/*.xml",
100: * "assembly/*.xml") will establish the current directory as the root,
101: * process all xml files in the assembly/boot directory before processing
102: * all xml files in the assembly directory itself.
103: *
104: * @param appRoot
105: * working directory
106: * @param bootConfig
107: * boot (primary) file or files (wildcards are allowed)
108: * @param appConfig
109: * application (secondary) file or files (wildcards are allowed)
110: */
111: public JetspeedSerializerSecondaryImpl(String appRoot,
112: String[] bootConfig, String[] appConfig)
113: throws SerializerException {
114: super (appRoot, bootConfig, appConfig);
115: }
116:
117: /**
118: * reset instruction flags to default settings (all true)
119: *
120: */
121: protected void resetSettings() {
122: setSetting(JetspeedSerializer.KEY_PROCESS_USERS, false);
123: setSetting(JetspeedSerializer.KEY_PROCESS_CAPABILITIES, false);
124: setSetting(JetspeedSerializer.KEY_PROCESS_PROFILER, false);
125: setSetting(JetspeedSerializer.KEY_PROCESS_PERMISSIONS, false);
126: setSetting(JetspeedSerializer.KEY_PROCESS_USER_PREFERENCES,
127: false);
128: setSetting(JetspeedSerializer.KEY_OVERWRITE_EXISTING, true);
129: setSetting(JetspeedSerializer.KEY_BACKUP_BEFORE_PROCESS, true);
130: }
131:
132: /**
133: * On import, get the basic SnapShot data
134: *
135: */
136: protected void getSnapshotData() {
137: logMe("date created : "
138: + ((JSSecondaryData) getSnapshot()).getDateCreated());
139: logMe("software Version : "
140: + ((JSSecondaryData) getSnapshot()).getSavedVersion());
141: logMe("software SUbVersion : "
142: + ((JSSecondaryData) getSnapshot())
143: .getSavedSubversion());
144: }
145:
146: /**
147: * On export, set the basic SnapShot data
148: *
149: */
150: protected void setSnapshotData() {
151: super .setSnapshotData();
152: }
153:
154: private JSPortlet exportPD(PortletDefinition pd)
155: throws SerializerException {
156:
157: try {
158: Collection col = entityAccess.getPortletEntities(pd);
159: if ((col == null) || (col.size() == 0))
160: return null;
161: JSPortlet portlet = new JSPortlet();
162: portlet.setName(pd.getName());
163: Iterator list = null;
164: try {
165: list = col.iterator();
166: } catch (Exception e) {
167: throw new SerializerException(
168: SerializerException.GET_EXISTING_OBJECTS
169: .create(new String[] { "entityAccess",
170: e.getMessage() }));
171: }
172: JSEntities entities = new JSEntities();
173:
174: while (list.hasNext()) {
175: MutablePortletEntity entity = (MutablePortletEntity) list
176: .next();
177: JSEntity jsEntity = exportEntityPref(entity);
178: if (jsEntity != null)
179: entities.add(jsEntity);
180:
181: }
182: System.out.println("-----processedAnyEntities for PD="
183: + pd.getName());
184: portlet.setEntities(entities);
185: return portlet;
186:
187: } catch (Exception e) {
188: throw new SerializerException(
189: SerializerException.CREATE_SERIALIZED_OBJECT_FAILED
190: .create(new String[] { "Entity",
191: e.getMessage() }));
192: }
193: }
194:
195: JSEntity exportEntityPref(MutablePortletEntity entity) {
196: JSEntity jsEntity = new JSEntity();
197: jsEntity.setId(entity.getId().toString());
198: String rootForEntity = MutablePortletEntity.PORTLET_ENTITY_ROOT
199: + "/" + entity.getId();
200: try {
201: if (!(Preferences.userRoot().nodeExists(rootForEntity))) {
202: // System.out.println("No preferences exist for entity "+
203: // entity.getId());
204: return jsEntity;
205: }
206:
207: Preferences prefNode = Preferences.userRoot().node(
208: rootForEntity);
209: String[] children = prefNode.childrenNames();
210: if ((children != null) && (children.length > 0)) {
211: JSEntityPreferences permissions = new JSEntityPreferences();
212:
213: for (int i = 0; i < children.length; i++) {
214: JSEntityPreference permission = processPreferenceNode(
215: entity, children[i]);
216: if (permission != null)
217: permissions.add(permission);
218: }
219: System.out.println("processed preferences for entity="
220: + entity.getId());
221: jsEntity.setEntityPreferences(permissions);
222: return jsEntity;
223: // processPreferenceNode(entity,prefNode,null);
224: }
225: return jsEntity;
226: } catch (Exception e) {
227: e.printStackTrace();
228: return null;
229: }
230:
231: }
232:
233: JSEntityPreference processPreferenceNode(
234: MutablePortletEntity entity, String child) {
235: String prefNodePath = MutablePortletEntity.PORTLET_ENTITY_ROOT
236: + "/" + entity.getId() + "/" + child + "/"
237: + PrefsPreference.PORTLET_PREFERENCES_ROOT;
238: Preferences prefNode = Preferences.userRoot()
239: .node(prefNodePath);
240:
241: if (prefNode == null)
242: return null;
243: JSEntityPreference permission = new JSEntityPreference();
244: permission.setName(child);
245:
246: try {
247: PrefsPreferenceSetImpl preferenceSet = new PrefsPreferenceSetImpl(
248: prefNode);
249: if (preferenceSet.size() == 0)
250: return null;
251: Iterator it = preferenceSet.iterator();
252: JSNVPElements v = new JSNVPElements();
253:
254: while (it.hasNext()) {
255: Preference pref = (Preference) it.next();
256: String name = pref.getName();
257: Iterator ii = pref.getValues();
258: while (ii.hasNext()) {
259: Object o = ii.next();
260: v.add(name, o.toString());
261: }
262: }
263: if (v.size() > 0) {
264: permission.setPreferences(v);
265: return permission;
266: }
267: return null;
268: } catch (Exception e) {
269: e.printStackTrace();
270: return null;
271:
272: }
273:
274: }
275:
276: private JSApplication exportPA(MutablePortletApplication pa)
277: throws SerializerException {
278:
279: JSApplication app = new JSApplication();
280: System.out.println("--processed PA " + pa.getName()
281: + " with id=" + pa.getId());
282: app.setID(pa.getId().toString());
283: app.setName(pa.getName());
284: /**
285: * while more PAs for each portletDef
286: * list:entityMan:getPortletEntity(pd)
287: */
288: PortletDefinitionList portletList = pa
289: .getPortletDefinitionList(); // .get(JetspeedObjectID.createFromString(TEST_PORTLET));
290: Iterator pi = portletList.iterator();
291: PortletDefinition pd = null;
292:
293: JSPortlets portlets = new JSPortlets();
294: while (pi.hasNext()) {
295: try {
296: pd = (PortletDefinition) pi.next();
297: JSPortlet p = exportPD(pd);
298: if (p != null) {
299: System.out.println("--processed PA " + pa.getName()
300: + " with pd=" + pd.getName());
301: portlets.add(p);
302: } else
303: System.out.println("--processed PA " + pa.getName()
304: + " with NULL pd=" + pd.getName());
305:
306: } catch (Exception e) {
307: throw new SerializerException(
308: SerializerException.CREATE_SERIALIZED_OBJECT_FAILED
309: .create(new String[] {
310: "PortletDefinition",
311: e.getMessage() }));
312: }
313: }
314: app.setPortlets(portlets);
315: return app;
316: }
317:
318: private JSApplications exportEntities() throws SerializerException {
319: registry = (PortletRegistry) getCM()
320: .getComponent(
321: "org.apache.jetspeed.components.portletregistry.PortletRegistry");
322: if (registry == null)
323: throw new SerializerException(
324: SerializerException.COMPONENTMANAGER_DOES_NOT_EXIST
325: .create("org.apache.jetspeed.components.portletregistry.PortletRegistry"));
326: Object o = getCM()
327: .getComponent(
328: "org.apache.jetspeed.components.portletentity.PortletEntityAccessComponent");
329: this .entityAccess = (PortletEntityAccessComponent) o;
330: if (entityAccess == null)
331: throw new SerializerException(
332: SerializerException.COMPONENTMANAGER_DOES_NOT_EXIST
333: .create("org.apache.jetspeed.components.portletentity.PortletEntityAccessComponent"));
334:
335: JSApplications applications = new JSApplications();
336:
337: Collection col = registry.getPortletApplications();
338: if ((col == null) || (col.size() == 0))
339: return applications;
340: Iterator list = null;
341: try {
342: list = col.iterator();
343: } catch (Exception e) {
344: throw new SerializerException(
345: SerializerException.GET_EXISTING_OBJECTS
346: .create(new String[] { "registry",
347: e.getMessage() }));
348: }
349: while (list.hasNext()) {
350: try {
351: MutablePortletApplication pa = (MutablePortletApplication) list
352: .next();
353: // PortletApplicationDefinition pa =
354: // (PortletApplicationDefinition)list.next();
355: applications.add(exportPA(pa));
356: } catch (Exception e) {
357: throw new SerializerException(
358: SerializerException.CREATE_SERIALIZED_OBJECT_FAILED
359: .create(new String[] {
360: "PortletApplicationDefinition",
361: e.getMessage() }));
362: }
363: }
364:
365: return applications;
366: }
367:
368: /**
369: * The workhorse for importing data
370: *
371: * @param binding
372: * established XML binding
373: * @return
374: * @throws SerializerException
375: */
376: protected void processImport() throws SerializerException {
377: this .logMe("*********reinstalling data*********");
378:
379: logMe("creating entities");
380: importEntities();
381: }
382:
383: /**
384: * The workhorse for exporting data
385: *
386: * @param binding
387: * established XML binding
388: * @return
389: * @throws SerializerException
390: */
391: protected void processExport(String name, XMLBinding binding)
392: throws SerializerException {
393: this .logMe("*********collecting data*********");
394: /** first create the snapshot file */
395:
396: this .setSnapshot(new JSSecondaryData(name));
397:
398: setSnapshotData();
399:
400: JSApplications apps = exportEntities();
401: ((JSSecondaryData) this .getSnapshot()).setApplications(apps);
402: /**
403: *
404: * if (this.getSetting(JetspeedSerializer.KEY_PROCESS_ENTITIES)) {
405: * logMe("collecting entities"); exportEntities(); } else
406: * logMe("entities skipped");
407: *
408: * if (this.getSetting(JetspeedSerializer.KEY_PROCESS_PREFERENCES)) {
409: * logMe("collecting preferences"); exportPreferences(); } else
410: * logMe("preferences skipped");
411: */
412:
413: }
414:
415: /**
416: * Setup the binding for the different classes, mapping each extracted class
417: * to a unique tag name in the XML
418: *
419: * @param binding
420: */
421: protected void setupAliases(XMLBinding binding) {
422: binding.setAlias(JSApplication.class, "PortletApplication");
423: binding.setAlias(JSApplications.class, "PortletApplications");
424: binding.setAlias(JSPortlet.class, "Portlet");
425: binding.setAlias(JSPortlets.class, "Portlets");
426: binding.setAlias(JSEntity.class, "Entity");
427: binding.setAlias(JSEntities.class, "Entities");
428: binding.setAlias(JSEntityPreference.class, "Principal");
429: binding.setAlias(JSEntityPreferences.class, "Settings");
430: binding.setAlias(JSSecondaryData.class, "RegistryData");
431: binding.setAlias(JSNVPElements.class, "preferences");
432:
433: binding.setAlias(String.class, "String");
434: binding.setAlias(Integer.class, "int");
435: binding.setClassAttribute(null);
436:
437: }
438:
439: private void importEntities() throws SerializerException {
440: overwrite = getSetting(JetspeedSerializer.KEY_OVERWRITE_EXISTING);
441:
442: registry = (PortletRegistry) getCM()
443: .getComponent(
444: "org.apache.jetspeed.components.portletregistry.PortletRegistry");
445: if (registry == null)
446: throw new SerializerException(
447: SerializerException.COMPONENTMANAGER_DOES_NOT_EXIST
448: .create("org.apache.jetspeed.components.portletregistry.PortletRegistry"));
449: Object o = getCM()
450: .getComponent(
451: "org.apache.jetspeed.components.portletentity.PortletEntityAccessComponent");
452: this .entityAccess = (PortletEntityAccessComponent) o;
453: if (entityAccess == null)
454: throw new SerializerException(
455: SerializerException.COMPONENTMANAGER_DOES_NOT_EXIST
456: .create("org.apache.jetspeed.components.portletentity.PortletEntityAccessComponent"));
457:
458: JSApplications applications = ((JSSecondaryData) this
459: .getSnapshot()).getApplications();
460:
461: if (applications == null) {
462: System.out.println("NO DATA!!!!!!");
463: return;
464: }
465: Iterator it = applications.iterator();
466: while (it.hasNext()) {
467: JSApplication app = (JSApplication) it.next();
468: MutablePortletApplication portletApp = registry
469: .getPortletApplication(app.getName());
470: if (portletApp != null) {
471: importPA(app, portletApp);
472: }
473: }
474: }
475:
476: void importPA(JSApplication app, MutablePortletApplication pa)
477: throws SerializerException {
478:
479: System.out.println("--processed PA " + pa.getName()
480: + " with id=" + pa.getId());
481: /**
482: * while more PAs for each portletDef
483: * list:entityMan:getPortletEntity(pd)
484: */
485:
486: Iterator pi = app.getPortlets().iterator();
487: while (pi.hasNext()) {
488: JSPortlet portlet = (JSPortlet) pi.next();
489: PortletDefinition pd = pa
490: .getPortletDefinitionByName(portlet.getName());
491: if (pd != null) {
492: importPD(portlet, pd);
493: }
494: }
495: }
496:
497: private void importPD(JSPortlet portlet, PortletDefinition pd)
498: throws SerializerException {
499:
500: JSEntities entities = portlet.getEntities();
501: Iterator it = entities.iterator();
502: while (it.hasNext()) {
503: JSEntity entity = (JSEntity) it.next();
504: MutablePortletEntity portletEntity = entityAccess
505: .getPortletEntity(entity.getId());
506: if (portletEntity == null) {
507: portletEntity = entityAccess.newPortletEntityInstance(
508: pd, entity.getId());
509: try {
510: entityAccess.storePortletEntity(portletEntity);
511: } catch (Exception e) {
512: e.printStackTrace();
513: }
514: }
515: // check preferences
516:
517: importEntityPref(entity, portletEntity);
518: }
519: }
520:
521: private void importEntityPref(JSEntity entity,
522: MutablePortletEntity portletEntity) {
523:
524: // do I carry any preferences?
525: JSEntityPreferences preferences = entity.getEntityPreferences();
526: if ((preferences == null) || (preferences.size() == 0))
527: return;
528:
529: //since I do have preferences let us make sure we have a root node
530:
531: String rootForEntity = MutablePortletEntity.PORTLET_ENTITY_ROOT
532: + "/" + portletEntity.getId();
533: try {
534: Preferences.userRoot().node(rootForEntity); // will create it if it doesn't exist
535:
536: Iterator it = preferences.iterator();
537: while (it.hasNext()) {
538: JSEntityPreference preference = (JSEntityPreference) it
539: .next();
540:
541: // do we have preferences for this one?
542: importPreferenceNode(preference, portletEntity);
543: }
544:
545: } catch (Exception e) {
546: e.printStackTrace();
547: return;
548: }
549:
550: }
551:
552: private void importPreferenceNode(JSEntityPreference preference,
553: MutablePortletEntity entity) {
554:
555: String child = preference.getName();
556:
557: String prefNodePath = MutablePortletEntity.PORTLET_ENTITY_ROOT
558: + "/" + entity.getId() + "/" + child + "/"
559: + PrefsPreference.PORTLET_PREFERENCES_ROOT;
560: Preferences prefNode = Preferences.userRoot()
561: .node(prefNodePath);
562:
563: if (prefNode == null)
564: return;
565:
566: JSNVPElements prefList = preference.getPreferences();
567: try {
568: PrefsPreferenceSetImpl preferenceSet = new PrefsPreferenceSetImpl(
569: prefNode);
570:
571: Iterator it = prefList.getMyMap().keySet().iterator();
572:
573: while (it.hasNext()) {
574: String key = (String) it.next();
575: String value = (String) prefList.getMyMap().get(key);
576: Preference p = preferenceSet.get(key);
577: if ((p == null) || (overwrite)) {
578:
579: Vector v = new Vector();
580: v.add(value);
581: preferenceSet.add(key, v);
582: System.out.println("Entity " + entity.getId()
583: + " updated with preference " + key + "="
584: + value);
585: }
586: }
587: preferenceSet.flush();
588: return;
589: } catch (Exception e) {
590: e.printStackTrace();
591: return;
592:
593: }
594:
595: }
596:
597: }
|