0001: /*
0002: * Licensed to the Apache Software Foundation (ASF) under one or more
0003: * contributor license agreements. See the NOTICE file distributed with
0004: * this work for additional information regarding copyright ownership.
0005: * The ASF licenses this file to You under the Apache License, Version 2.0
0006: * (the "License"); you may not use this file except in compliance with
0007: * the License. You may obtain a copy of the License at
0008: *
0009: * http://www.apache.org/licenses/LICENSE-2.0
0010: *
0011: * Unless required by applicable law or agreed to in writing, software
0012: * distributed under the License is distributed on an "AS IS" BASIS,
0013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0014: * See the License for the specific language governing permissions and
0015: * limitations under the License.
0016: */
0017: package org.apache.jetspeed.serializer;
0018:
0019: import java.security.Principal;
0020: import java.util.ArrayList;
0021: import java.util.Collection;
0022: import java.util.HashMap;
0023: import java.util.Iterator;
0024: import java.util.Map;
0025: import java.util.Set;
0026: import java.util.prefs.Preferences;
0027:
0028: import javax.security.auth.Subject;
0029:
0030: import javolution.xml.XMLBinding;
0031:
0032: import org.apache.jetspeed.capabilities.Capabilities;
0033: import org.apache.jetspeed.capabilities.Capability;
0034: import org.apache.jetspeed.capabilities.Client;
0035: import org.apache.jetspeed.capabilities.MediaType;
0036: import org.apache.jetspeed.capabilities.MimeType;
0037: import org.apache.jetspeed.components.ComponentManager;
0038: import org.apache.jetspeed.profiler.Profiler;
0039: import org.apache.jetspeed.profiler.ProfilerException;
0040: import org.apache.jetspeed.profiler.rules.PrincipalRule;
0041: import org.apache.jetspeed.profiler.rules.ProfilingRule;
0042: import org.apache.jetspeed.profiler.rules.RuleCriterion;
0043: import org.apache.jetspeed.security.BasePrincipal;
0044: import org.apache.jetspeed.security.Group;
0045: import org.apache.jetspeed.security.GroupManager;
0046: import org.apache.jetspeed.security.PasswordCredential;
0047: import org.apache.jetspeed.security.PermissionManager;
0048: import org.apache.jetspeed.security.PortalResourcePermission;
0049: import org.apache.jetspeed.security.Role;
0050: import org.apache.jetspeed.security.RoleManager;
0051: import org.apache.jetspeed.security.User;
0052: import org.apache.jetspeed.security.UserManager;
0053: import org.apache.jetspeed.security.om.InternalPermission;
0054: import org.apache.jetspeed.security.om.InternalPrincipal;
0055: import org.apache.jetspeed.security.spi.PasswordCredentialProvider;
0056: import org.apache.jetspeed.serializer.objects.JSCapabilities;
0057: import org.apache.jetspeed.serializer.objects.JSCapability;
0058: import org.apache.jetspeed.serializer.objects.JSClient;
0059: import org.apache.jetspeed.serializer.objects.JSClientCapabilities;
0060: import org.apache.jetspeed.serializer.objects.JSClientMimeTypes;
0061: import org.apache.jetspeed.serializer.objects.JSClients;
0062: import org.apache.jetspeed.serializer.objects.JSGroup;
0063: import org.apache.jetspeed.serializer.objects.JSGroups;
0064: import org.apache.jetspeed.serializer.objects.JSMediaType;
0065: import org.apache.jetspeed.serializer.objects.JSMediaTypes;
0066: import org.apache.jetspeed.serializer.objects.JSMimeType;
0067: import org.apache.jetspeed.serializer.objects.JSMimeTypes;
0068: import org.apache.jetspeed.serializer.objects.JSNVPElements;
0069: import org.apache.jetspeed.serializer.objects.JSPWAttributes;
0070: import org.apache.jetspeed.serializer.objects.JSPermission;
0071: import org.apache.jetspeed.serializer.objects.JSPermissions;
0072: import org.apache.jetspeed.serializer.objects.JSPrincipalRule;
0073: import org.apache.jetspeed.serializer.objects.JSPrincipalRules;
0074: import org.apache.jetspeed.serializer.objects.JSProfilingRule;
0075: import org.apache.jetspeed.serializer.objects.JSProfilingRules;
0076: import org.apache.jetspeed.serializer.objects.JSRole;
0077: import org.apache.jetspeed.serializer.objects.JSRoles;
0078: import org.apache.jetspeed.serializer.objects.JSRuleCriterion;
0079: import org.apache.jetspeed.serializer.objects.JSRuleCriterions;
0080: import org.apache.jetspeed.serializer.objects.JSSeedData;
0081: import org.apache.jetspeed.serializer.objects.JSSnapshot;
0082: import org.apache.jetspeed.serializer.objects.JSUser;
0083: import org.apache.jetspeed.serializer.objects.JSUserAttributes;
0084: import org.apache.jetspeed.serializer.objects.JSUserGroups;
0085: import org.apache.jetspeed.serializer.objects.JSUserRoles;
0086: import org.apache.jetspeed.serializer.objects.JSUserUsers;
0087: import org.apache.jetspeed.serializer.objects.JSUsers;
0088:
0089: /**
0090: * Jetspeed Serializer
0091: * <p>
0092: * The Serializer is capable of reading and writing the current content of the
0093: * Jetspeed environment to and from XML files. The component can be used from a
0094: * standalone java application for seeding a new database or from a running
0095: * portal as an administrative backup/restore function.
0096: * <p>
0097: * The XML file needs to indicate whether passwords used in credentials
0098: * are plain text or whether they are encoded. The import algoritm can determine -
0099: * prior to reading users - which encode/decode scheme was used and if <none> or
0100: * <implements PasswordEncodingService> then we store plain passwords (Note that
0101: * that alone requires the resulting XML to be encoded!!!!!)
0102: *
0103: * @author <a href="mailto:hajo@bluesunrise.com">Hajo Birthelmer</a>
0104: * @version $Id: $
0105: */
0106: public class JetspeedSerializerImpl extends JetspeedSerializerBase
0107: implements JetspeedSerializer {
0108:
0109: private HashMap roleMap = new HashMap();
0110:
0111: private HashMap groupMap = new HashMap();
0112:
0113: private HashMap userMap = new HashMap();
0114:
0115: private HashMap mimeMap = new HashMap();
0116: private HashMap mimeMapInt = new HashMap();
0117:
0118: private HashMap mediaMap = new HashMap();
0119:
0120: private HashMap capabilityMap = new HashMap();
0121: private HashMap capabilityMapInt = new HashMap();
0122:
0123: private HashMap clientMap = new HashMap();
0124:
0125: private HashMap permissionMap = new HashMap();
0126:
0127: private HashMap rulesMap = new HashMap();
0128:
0129: int refCouter = 0;
0130:
0131: private static String ENCODING_STRING = "JETSPEED 2.1 - 2006";
0132: private static String JETSPEED = "JETSPEED";
0133:
0134: protected Class getSerializerDataClass() {
0135: return JSSeedData.class;
0136: }
0137:
0138: protected String getSerializerDataTag() {
0139: return TAG_SNAPSHOT;
0140: }
0141:
0142: public JetspeedSerializerImpl() {
0143: super ();
0144: }
0145:
0146: /**
0147: * hand over existing component manager
0148: *
0149: * @param cm
0150: */
0151: public JetspeedSerializerImpl(ComponentManager cm) {
0152: super (cm);
0153: }
0154:
0155: /**
0156: * This constructor takes the application root, the search path for the boot
0157: * component configuration files and the search path for the application
0158: * component configuration files.
0159: * <p>
0160: * For example: new JetspeedSerializerImpl("./", "assembly/boot/*.xml",
0161: * "assembly/*.xml") will establish the current directory as the root,
0162: * process all xml files in the assembly/boot directory before processing
0163: * all xml files in the assembly directory itself.
0164: *
0165: * @param appRoot
0166: * working directory
0167: * @param bootConfig
0168: * boot (primary) file or files (wildcards are allowed)
0169: * @param appConfig
0170: * application (secondary) file or files (wildcards are allowed)
0171: */
0172: public JetspeedSerializerImpl(String appRoot, String[] bootConfig,
0173: String[] appConfig) throws SerializerException {
0174: super (appRoot, bootConfig, appConfig);
0175: }
0176:
0177: /**
0178: * reset instruction flags to default settings (all true)
0179: *
0180: */
0181: protected void resetSettings() {
0182: setSetting(JetspeedSerializer.KEY_PROCESS_USERS, true);
0183: setSetting(JetspeedSerializer.KEY_PROCESS_CAPABILITIES, true);
0184: setSetting(JetspeedSerializer.KEY_PROCESS_PROFILER, true);
0185: setSetting(JetspeedSerializer.KEY_PROCESS_PERMISSIONS, true);
0186: setSetting(JetspeedSerializer.KEY_OVERWRITE_EXISTING, true);
0187: setSetting(JetspeedSerializer.KEY_BACKUP_BEFORE_PROCESS, true);
0188: }
0189:
0190: /**
0191: * On import, get the basic SnapShot data
0192: *
0193: */
0194: protected void getSnapshotData() {
0195: logMe("date created : " + getSnapshot().getDateCreated());
0196: logMe("software Version : " + getSnapshot().getSavedVersion());
0197: logMe("software SUbVersion : "
0198: + getSnapshot().getSavedSubversion());
0199: }
0200:
0201: private void recreateCapabilities(Capabilities caps)
0202: throws SerializerException {
0203: logMe("recreateCapabilities - processing");
0204: JSCapabilities capabilities = ((JSSeedData) getSnapshot())
0205: .getCapabilities();
0206: if ((capabilities != null) && (capabilities.size() > 0)) {
0207: Iterator _it = capabilities.iterator();
0208: while (_it.hasNext()) {
0209: JSCapability _c = (JSCapability) _it.next();
0210: // create a new Capability
0211: try {
0212: Capability capability = caps.createCapability(_c
0213: .getName());
0214: /** THE KEY_OVERWRITE_EXISTING test is not required for capabilites, since they carry no other information than the name
0215: * Used here for consistency, though
0216: */
0217: if ((this
0218: .getSetting(JetspeedSerializer.KEY_OVERWRITE_EXISTING))
0219: || (capability.getCapabilityId() == 0)) {
0220: caps.storeCapability(capability);
0221: }
0222: this .capabilityMap.put(_c.getName(), capability);
0223: } catch (Exception e) {
0224: throw new SerializerException(
0225: SerializerException.CREATE_OBJECT_FAILED
0226: .create(
0227: "org.apache.jetspeed.capabilities.Capabilities",
0228: e.getLocalizedMessage()));
0229: }
0230: }
0231: } else
0232: logMe("NO CAPABILITES?????");
0233: logMe("recreateCapabilities - done");
0234: }
0235:
0236: private void recreateMimeTypes(Capabilities caps)
0237: throws SerializerException {
0238: logMe("recreateMimeTypes - processing");
0239: JSMimeTypes mimeTypes = ((JSSeedData) getSnapshot())
0240: .getMimeTypes();
0241: if ((mimeTypes != null) && (mimeTypes.size() > 0)) {
0242: Iterator _it = mimeTypes.iterator();
0243: while (_it.hasNext()) {
0244: JSMimeType _c = (JSMimeType) _it.next();
0245: // create a new Mime Type
0246: try {
0247: MimeType mimeType = caps.createMimeType(_c
0248: .getName());
0249: /** THE KEY_OVERWRITE_EXISTING test is not required for mime types, since they carry no other information than the name
0250: * Used here for consistency, though
0251: */
0252: if ((this
0253: .getSetting(JetspeedSerializer.KEY_OVERWRITE_EXISTING))
0254: || (mimeType.getMimetypeId() == 0)) {
0255: caps.storeMimeType(mimeType);
0256: }
0257: this .mimeMap.put(_c.getName(), mimeType);
0258:
0259: } catch (Exception e) {
0260: throw new SerializerException(
0261: SerializerException.CREATE_OBJECT_FAILED
0262: .create(
0263: "org.apache.jetspeed.capabilities.MimeType",
0264: e.getLocalizedMessage()));
0265: }
0266: }
0267: } else
0268: logMe("NO MIME TYPES?????");
0269: logMe("recreateMimeTypes - done");
0270: }
0271:
0272: private void recreateMediaTypes(Capabilities caps)
0273: throws SerializerException {
0274: String _line;
0275:
0276: logMe("recreateMediaTypes - processing");
0277: JSMediaTypes mediaTypes = ((JSSeedData) getSnapshot())
0278: .getMediaTypes();
0279: if ((mediaTypes != null) && (mediaTypes.size() > 0)) {
0280: Iterator _it = mediaTypes.iterator();
0281: while (_it.hasNext()) {
0282: JSMediaType _c = (JSMediaType) _it.next();
0283: // create a new Media
0284: try {
0285: MediaType mediaType = caps.createMediaType(_c
0286: .getName());
0287: /** THE KEY_OVERWRITE_EXISTING test IS required for media types, since they carry no other information than the name
0288: * Used here for consistency, though
0289: */
0290: if ((this
0291: .getSetting(JetspeedSerializer.KEY_OVERWRITE_EXISTING))
0292: || (mediaType.getMediatypeId() == 0)) {
0293: // set object fields
0294: mediaType.setCharacterSet(_c.getCharacterSet());
0295: mediaType.setTitle(_c.getTitle());
0296: mediaType.setDescription(_c.getDescription());
0297:
0298: try {
0299: _line = _c.getMimeTypesString().toString();
0300: ArrayList list = this .getTokens(_line);
0301: if ((list != null) && (list.size() > 0)) {
0302: Iterator _it1 = list.iterator();
0303: int added = 0;
0304: while (_it1.hasNext()) {
0305: MimeType _mt = caps
0306: .createMimeType((String) _it1
0307: .next());
0308: if (_mt != null)
0309: mediaType.addMimetype(_mt);
0310: added++;
0311: }
0312: }
0313: } catch (Exception e1) {
0314: e1.printStackTrace();
0315: }
0316: try {
0317: _line = _c.getCapabilitiesString()
0318: .toString();
0319: ArrayList list = this .getTokens(_line);
0320: if ((list != null) && (list.size() > 0)) {
0321: Iterator _it1 = list.iterator();
0322: if ((list != null) && (list.size() > 0)) {
0323: int added = 0;
0324: while (_it1.hasNext()) {
0325: Capability _ct = caps
0326: .createCapability((String) _it1
0327: .next());
0328: if (_ct != null)
0329: mediaType
0330: .addCapability(_ct);
0331: added++;
0332: }
0333: }
0334: }
0335: } catch (Exception e1) {
0336: e1.printStackTrace();
0337: }
0338: caps.storeMediaType(mediaType);
0339: }
0340: this .mediaMap.put(_c.getName(), mediaType);
0341: } catch (Exception e) {
0342: throw new SerializerException(
0343: SerializerException.CREATE_OBJECT_FAILED
0344: .create(
0345: "org.apache.jetspeed.capabilities.MediaType",
0346: e.getLocalizedMessage()));
0347: }
0348: }
0349: } else
0350: logMe("NO MEDIA TYPES?????");
0351: logMe("recreateMediaTypes - done");
0352: }
0353:
0354: private void recreateClients(Capabilities caps)
0355: throws SerializerException {
0356: String _line;
0357:
0358: logMe("recreateClients - processing");
0359: JSClients clients = ((JSSeedData) getSnapshot()).getClients();
0360: if ((clients != null) && (clients.size() > 0)) {
0361: Iterator _it = clients.iterator();
0362: while (_it.hasNext()) {
0363: JSClient _c = (JSClient) _it.next();
0364: // create a new Media
0365: try {
0366: Client client = caps.createClient(_c.getName());
0367: /** THE KEY_OVERWRITE_EXISTING test IS required for media types, since they carry no other information than the name
0368: * Used here for consistency, though
0369: */
0370: if ((this
0371: .getSetting(JetspeedSerializer.KEY_OVERWRITE_EXISTING))
0372: || (client.getClientId() == 0)) {
0373: // set object fields
0374: client.setUserAgentPattern(_c
0375: .getUserAgentPattern());
0376: client.setManufacturer(_c.getManufacturer());
0377: client.setModel(_c.getModel());
0378: client.setEvalOrder(_c.getEvalOrder());
0379: String myPrefMimeType = _c
0380: .getPreferredMimeTypeID();
0381: client.setVersion(_c.getVersion());
0382: try {
0383: _line = _c.getMimeTypesString().toString();
0384: ArrayList list = this .getTokens(_line);
0385: if ((list != null) && (list.size() > 0)) {
0386: Iterator _it1 = list.iterator();
0387: int added = 0;
0388: while (_it1.hasNext()) {
0389: MimeType _mt = caps
0390: .createMimeType((String) _it1
0391: .next());
0392: if (_mt != null) {
0393: client.getMimetypes().add(_mt);
0394: if (_mt.getMimetypeId() == 0) {
0395: caps.storeMimeType(_mt);
0396: }
0397: if (myPrefMimeType
0398: .equalsIgnoreCase(_mt
0399: .getName()))
0400: client
0401: .setPreferredMimeTypeId(_mt
0402: .getMimetypeId());
0403:
0404: }
0405: added++;
0406: }
0407: }
0408: } catch (Exception e1) {
0409: e1.printStackTrace();
0410: }
0411: try {
0412: _line = _c.getCapabilitiesString()
0413: .toString();
0414: ArrayList list = this .getTokens(_line);
0415: if ((list != null) && (list.size() > 0)) {
0416: Iterator _it1 = list.iterator();
0417: if ((list != null) && (list.size() > 0)) {
0418: int added = 0;
0419: while (_it1.hasNext()) {
0420: Capability _ct = caps
0421: .createCapability((String) _it1
0422: .next());
0423: if (_ct != null)
0424: client.getCapabilities()
0425: .add(_ct);
0426: added++;
0427: }
0428: }
0429: }
0430: } catch (Exception e1) {
0431: e1.printStackTrace();
0432: }
0433: caps.storeClient(client);
0434: }
0435: this .clientMap.put(_c.getName(), client);
0436: } catch (Exception e) {
0437: throw new SerializerException(
0438: SerializerException.CREATE_OBJECT_FAILED
0439: .create(
0440: "org.apache.jetspeed.capabilities.Client",
0441: e.getLocalizedMessage()));
0442: }
0443: }
0444: } else
0445: logMe("NO MEDIA TYPES?????");
0446: logMe("recreateClients - done");
0447: }
0448:
0449: private void importCapabilitiesInfrastructure()
0450: throws SerializerException {
0451: logMe("importCapabilitiesInfrastructure - processing");
0452: Capabilities caps = (Capabilities) getCM().getComponent(
0453: "org.apache.jetspeed.capabilities.Capabilities");
0454: if (caps == null)
0455: throw new SerializerException(
0456: SerializerException.COMPONENTMANAGER_DOES_NOT_EXIST
0457: .create("org.apache.jetspeed.capabilities.Capabilities"));
0458:
0459: recreateCapabilities(caps);
0460: recreateMimeTypes(caps);
0461: recreateMediaTypes(caps);
0462: recreateClients(caps);
0463:
0464: logMe("importCapabilitiesInfrastructure - processing done");
0465: }
0466:
0467: /**
0468: * import the groups, roles and finally the users to the current environment
0469: *
0470: * @throws SerializerException
0471: */
0472: private void recreateRolesGroupsUsers() throws SerializerException {
0473: logMe("recreateRolesGroupsUsers");
0474: GroupManager groupManager = (GroupManager) getCM()
0475: .getComponent(
0476: "org.apache.jetspeed.security.GroupManager");
0477: if (groupManager == null)
0478: throw new SerializerException(
0479: SerializerException.COMPONENTMANAGER_DOES_NOT_EXIST
0480: .create("org.apache.jetspeed.security.GroupManager"));
0481: RoleManager roleManager = (RoleManager) getCM().getComponent(
0482: "org.apache.jetspeed.security.RoleManager");
0483: if (roleManager == null)
0484: throw new SerializerException(
0485: SerializerException.COMPONENTMANAGER_DOES_NOT_EXIST
0486: .create("org.apache.jetspeed.security.RoleManager"));
0487: UserManager userManager = (UserManager) getCM().getComponent(
0488: "org.apache.jetspeed.security.UserManager");
0489: if (userManager == null)
0490: throw new SerializerException(
0491: SerializerException.COMPONENTMANAGER_DOES_NOT_EXIST
0492: .create("org.apache.jetspeed.security.UserManager"));
0493:
0494: JSGroups groups = null;
0495: JSRoles roles = null;
0496: Iterator _it = null;
0497: groups = ((JSSeedData) getSnapshot()).getGroups();
0498: if (groups != null) {
0499: _it = groups.iterator();
0500: while (_it.hasNext()) {
0501: String name = ((JSGroup) _it.next()).getName();
0502:
0503: try {
0504: if (!(groupManager.groupExists(name)))
0505: groupManager.addGroup(name);
0506: Group group = groupManager.getGroup(name);
0507: this .groupMap.put(name, group.getPrincipal());
0508: } catch (Exception e) {
0509: throw new SerializerException(
0510: SerializerException.CREATE_OBJECT_FAILED
0511: .create(new String[] { "Group",
0512: e.getMessage() }));
0513: }
0514: }
0515: }
0516: logMe("recreateGroups - done");
0517: logMe("processing roles");
0518:
0519: roles = ((JSSeedData) getSnapshot()).getRoles();
0520: if (roles != null) {
0521: _it = roles.iterator();
0522:
0523: while (_it.hasNext()) {
0524: String name = ((JSRole) _it.next()).getName();
0525:
0526: try {
0527: if (!(roleManager.roleExists(name)))
0528: roleManager.addRole(name);
0529: Role role = roleManager.getRole(name);
0530: this .roleMap.put(name, role.getPrincipal());
0531: } catch (Exception e) {
0532: throw new SerializerException(
0533: SerializerException.CREATE_OBJECT_FAILED
0534: .create(new String[] { "Role",
0535: e.getMessage() }));
0536: }
0537: }
0538: }
0539: logMe("recreateRoles - done");
0540: logMe("processing users");
0541:
0542: /** determine whether passwords can be reconstructed or not */
0543: int passwordEncoding = compareCurrentSecurityProvider((JSSeedData) getSnapshot());
0544: JSUsers users = null;
0545: users = ((JSSeedData) getSnapshot()).getUsers();
0546: if (users != null) {
0547: _it = users.iterator();
0548: while (_it.hasNext()) {
0549:
0550: JSUser jsuser = (JSUser) _it.next();
0551:
0552: try {
0553: User user = null;
0554: if (userManager.userExists(jsuser.getName())) {
0555: user = userManager.getUser(jsuser.getName());
0556: }
0557: if ((this
0558: .getSetting(JetspeedSerializer.KEY_OVERWRITE_EXISTING))
0559: || (user == null)) {
0560: if (user == null) //create new one
0561: {
0562: String password = recreatePassword(jsuser
0563: .getPassword());
0564: logMe("add User " + jsuser.getName()
0565: + " with password " + password);
0566: userManager
0567: .importUser(
0568: jsuser.getName(),
0569: password,
0570: (passwordEncoding == PASSTHRU_REQUIRED));
0571: logMe("add User done ");
0572: user = userManager
0573: .getUser(jsuser.getName());
0574: }
0575: try {
0576: userManager.setPasswordEnabled(jsuser
0577: .getName(), jsuser.getPwEnabled());
0578: userManager.setPasswordUpdateRequired(
0579: jsuser.getName(), jsuser
0580: .getPwRequiredUpdate());
0581: java.sql.Date d = jsuser
0582: .getPwExpirationDate();
0583: if (d != null)
0584: userManager.setPasswordExpiration(
0585: jsuser.getName(), d);
0586: } catch (Exception e) {
0587: // most likely caused by protected users (like "guest")
0588: logMe("setting userinfo for "
0589: + jsuser.getName()
0590: + " failed because of "
0591: + e.getLocalizedMessage());
0592: }
0593:
0594: //credentials
0595: Subject subject = user.getSubject();
0596:
0597: ArrayList listTemp = jsuser
0598: .getPrivateCredentials();
0599: if ((listTemp != null) && (listTemp.size() > 0)) {
0600: Iterator _itTemp = listTemp.iterator();
0601: while (_itTemp.hasNext()) {
0602: subject.getPrivateCredentials().add(
0603: _itTemp.next());
0604: }
0605: }
0606: listTemp = jsuser.getPublicCredentials();
0607: if ((listTemp != null) && (listTemp.size() > 0)) {
0608: Iterator _itTemp = listTemp.iterator();
0609: while (_itTemp.hasNext()) {
0610: subject.getPublicCredentials().add(
0611: _itTemp.next());
0612: }
0613: }
0614: JSUserGroups jsUserGroups = jsuser
0615: .getGroupString();
0616: if (jsUserGroups != null)
0617: listTemp = this .getTokens(jsUserGroups
0618: .toString());
0619: else
0620: listTemp = null;
0621: if ((listTemp != null) && (listTemp.size() > 0)) {
0622: Iterator _itTemp = listTemp.iterator();
0623: while (_itTemp.hasNext()) {
0624: groupManager.addUserToGroup(jsuser
0625: .getName(), (String) _itTemp
0626: .next());
0627: }
0628: }
0629: JSUserRoles jsUserRoles = jsuser
0630: .getRoleString();
0631: if (jsUserRoles != null)
0632: listTemp = this .getTokens(jsUserRoles
0633: .toString());
0634: else
0635: listTemp = null;
0636: if ((listTemp != null) && (listTemp.size() > 0)) {
0637: Iterator _itTemp = listTemp.iterator();
0638: while (_itTemp.hasNext()) {
0639: roleManager.addRoleToUser(jsuser
0640: .getName(), (String) _itTemp
0641: .next());
0642: }
0643: }
0644: JSUserAttributes attributes = jsuser
0645: .getUserInfo();
0646: if (attributes != null) {
0647: Preferences userAttributes = user
0648: .getUserAttributes();
0649: HashMap map = attributes.getMyMap();
0650: if (map != null) {
0651: Iterator _itTemp = map.keySet()
0652: .iterator();
0653: while (_itTemp.hasNext()) {
0654: String userAttrName = (String) _itTemp
0655: .next();
0656: // if ( userAttributes.get(userAttrName, "").equals("")
0657: String userAttrValue = (String) map
0658: .get(userAttrName);
0659: userAttributes.put(userAttrName,
0660: userAttrValue);
0661: }
0662: }
0663:
0664: }
0665:
0666: JSNVPElements jsNVP = jsuser.getPreferences();
0667: if ((jsNVP != null)
0668: && (jsNVP.getMyMap() != null)) {
0669: Preferences preferences = user
0670: .getPreferences();
0671: Iterator _itTemp = jsNVP.getMyMap()
0672: .keySet().iterator();
0673: while (_itTemp.hasNext()) {
0674: String prefKey = (String) _itTemp
0675: .next();
0676: String prefValue = (String) (jsNVP
0677: .getMyMap().get(prefKey));
0678: preferences.put(prefKey, prefValue);
0679: }
0680: }
0681:
0682: this .userMap.put(jsuser.getName(),
0683: getUserPrincipal(user));
0684:
0685: }
0686: } catch (Exception e) {
0687: e.printStackTrace();
0688: throw new SerializerException(
0689: SerializerException.CREATE_OBJECT_FAILED
0690: .create(new String[] { "User",
0691: e.getMessage() }));
0692: }
0693: }
0694: }
0695: logMe("recreateUsers - done");
0696: return;
0697: }
0698:
0699: /**
0700: * called only after users have been established
0701: * @throws SerializerException
0702: */
0703: private void recreateUserPrincipalRules()
0704: throws SerializerException {
0705: logMe("recreateUserPrincipalRules - started");
0706:
0707: Profiler pm = (Profiler) getCM().getComponent(
0708: "org.apache.jetspeed.profiler.Profiler");
0709: if (pm == null)
0710:
0711: throw new SerializerException(
0712: SerializerException.COMPONENTMANAGER_DOES_NOT_EXIST
0713: .create("org.apache.jetspeed.profiler.Profiler"));
0714: UserManager userManager = (UserManager) getCM().getComponent(
0715: "org.apache.jetspeed.security.UserManager");
0716: if (userManager == null)
0717: throw new SerializerException(
0718: SerializerException.COMPONENTMANAGER_DOES_NOT_EXIST
0719: .create("org.apache.jetspeed.security.UserManager"));
0720:
0721: // get Rules for each user
0722:
0723: Iterator _itUsers = ((JSSeedData) getSnapshot()).getUsers()
0724: .iterator();
0725: while (_itUsers.hasNext()) {
0726: JSUser _user = (JSUser) _itUsers.next();
0727: JSPrincipalRules jsRules = _user.getRules();
0728: try {
0729: User user = userManager.getUser(_user.getName());
0730: Principal principal = getUserPrincipal(user);
0731: if (jsRules != null) {
0732: Iterator _itRoles = jsRules.iterator();
0733: while (_itRoles.hasNext()) {
0734: JSPrincipalRule pr = (JSPrincipalRule) _itRoles
0735: .next();
0736: ProfilingRule pRule = pm.getRule(pr.getRule());
0737:
0738: try {
0739: PrincipalRule p1 = pm.createPrincipalRule();
0740: p1.setLocatorName(pr.getLocator());
0741: p1.setProfilingRule(pRule);
0742: p1.setPrincipalName(principal.getName());
0743: pm.storePrincipalRule(p1);
0744: } catch (Exception eRole) {
0745: eRole.printStackTrace();
0746: }
0747: }
0748: }
0749: } catch (Exception eUser) {
0750: eUser.printStackTrace();
0751: }
0752: }
0753: logMe("recreateUserPrincipalRules - done");
0754:
0755: }
0756:
0757: /**
0758: * recreates all permissions from the current snapshot
0759: *
0760: * @throws SerializerException
0761: */
0762: private void recreatePermissions() throws SerializerException {
0763: logMe("recreatePermissions - started");
0764: PermissionManager pm = (PermissionManager) getCM()
0765: .getComponent(
0766: "org.apache.jetspeed.security.PermissionManager");
0767: if (pm == null)
0768: throw new SerializerException(
0769: SerializerException.COMPONENTMANAGER_DOES_NOT_EXIST
0770: .create("org.apache.jetspeed.security.PermissionManager"));
0771:
0772: Iterator list = null;
0773: JSPermissions permissions = ((JSSeedData) getSnapshot())
0774: .getPermissions();
0775:
0776: if (permissions != null) {
0777: list = permissions.iterator();
0778: while (list.hasNext()) {
0779: JSPermission _js = (JSPermission) list.next();
0780: PortalResourcePermission perm = _js
0781: .getPermissionForType();
0782: if ((perm != null)
0783: && (perm instanceof PortalResourcePermission)) {
0784: try {
0785: pm.addPermission(perm);
0786: ArrayList listTemp = null;
0787: JSUserGroups jsUserGroups = _js
0788: .getGroupString();
0789: if (jsUserGroups != null)
0790: listTemp = this .getTokens(jsUserGroups
0791: .toString());
0792: else
0793: listTemp = null;
0794: if ((listTemp != null) && (listTemp.size() > 0)) {
0795: Iterator _itTemp = listTemp.iterator();
0796: while (_itTemp.hasNext()) {
0797: Principal p = (Principal) this .groupMap
0798: .get((String) _itTemp.next());
0799: if (p != null)
0800: pm.grantPermission(p, perm);
0801: }
0802: }
0803: JSUserRoles jsUserRoles = _js.getRoleString();
0804: if (jsUserRoles != null)
0805: listTemp = this .getTokens(jsUserRoles
0806: .toString());
0807: else
0808: listTemp = null;
0809: if ((listTemp != null) && (listTemp.size() > 0)) {
0810: Iterator _itTemp = listTemp.iterator();
0811: while (_itTemp.hasNext()) {
0812: Principal p = (Principal) this .roleMap
0813: .get((String) _itTemp.next());
0814: if (p != null)
0815: pm.grantPermission(p, perm);
0816: }
0817: }
0818: JSUserUsers jsUserUsers = _js.getUserString();
0819: if (jsUserUsers != null)
0820: listTemp = this .getTokens(jsUserUsers
0821: .toString());
0822: else
0823: listTemp = null;
0824: if ((listTemp != null) && (listTemp.size() > 0)) {
0825: Iterator _itTemp = listTemp.iterator();
0826: while (_itTemp.hasNext()) {
0827: Principal p = (Principal) this .userMap
0828: .get((String) _itTemp.next());
0829: if (p != null)
0830: pm.grantPermission(p, perm);
0831: }
0832: }
0833:
0834: } catch (Exception e) {
0835: throw new SerializerException(
0836: SerializerException.CREATE_SERIALIZED_OBJECT_FAILED
0837: .create(new String[] {
0838: "Permissions",
0839: e.getMessage() }));
0840: }
0841: }
0842: }
0843: }
0844: logMe("recreatePermissions - done");
0845: }
0846:
0847: private Principal getUserPrincipal(User user) {
0848: Subject subject = user.getSubject();
0849: // get the user principal
0850: Set principals = subject.getPrincipals();
0851: Iterator list = principals.iterator();
0852: while (list.hasNext()) {
0853: BasePrincipal principal = (BasePrincipal) list.next();
0854: String path = principal.getFullPath();
0855: if (path.startsWith("/user/"))
0856: return principal;
0857: }
0858: return null;
0859:
0860: }
0861:
0862: private void importProfiler() {
0863: System.out.println("importProfiler - processing");
0864: try {
0865: recreateProfilingRules();
0866: } catch (Exception e) {
0867: e.printStackTrace();
0868: }
0869: }
0870:
0871: private void importUserGroupRoles() {
0872: System.out.println("importUserGroupRoles - processing");
0873: try {
0874: recreateRolesGroupsUsers();
0875: } catch (Exception e) {
0876: e.printStackTrace();
0877: }
0878: }
0879:
0880: private void importUserPrincipals() {
0881: System.out.println("importUserPrincipals - processing");
0882: try {
0883: recreateUserPrincipalRules();
0884: } catch (Exception e) {
0885: e.printStackTrace();
0886: }
0887: }
0888:
0889: /**
0890: * The workhorse for importing data
0891: *
0892: * @param binding
0893: * established XML binding
0894: * @return
0895: * @throws SerializerException
0896: */
0897: protected void processImport() throws SerializerException {
0898: this .logMe("*********reinstalling data*********");
0899:
0900: // TODO: HJB Make sure to clean lookup tables before next run
0901:
0902: if (this
0903: .getSetting(JetspeedSerializer.KEY_PROCESS_CAPABILITIES)) {
0904: logMe("creating clients, mediatypes and mimetypes");
0905: importCapabilitiesInfrastructure();
0906: }
0907: /**
0908: * the order is important, since profiling rules are referenced by the user
0909: *
0910: */
0911: if (this .getSetting(JetspeedSerializer.KEY_PROCESS_PROFILER)) {
0912: logMe("collecting permissions, profiling rules and users/proups etc. etc.");
0913: importProfiler();
0914: }
0915: if (this .getSetting(JetspeedSerializer.KEY_PROCESS_USERS)) {
0916: logMe("creating users/roles/groups");
0917: this .importUserGroupRoles();
0918: }
0919: if (this .getSetting(JetspeedSerializer.KEY_PROCESS_PROFILER)
0920: || this
0921: .getSetting(JetspeedSerializer.KEY_PROCESS_USERS)) {
0922: logMe("collecting user principals");
0923: this .importUserPrincipals();
0924: }
0925:
0926: if (this .getSetting(JetspeedSerializer.KEY_PROCESS_PERMISSIONS)) {
0927: logMe("permissions, rules etc. skipped ");
0928: recreatePermissions();
0929: }
0930: }
0931:
0932: /**
0933: * The workhorse for exporting data
0934: *
0935: * @param binding
0936: * established XML binding
0937: * @return
0938: * @throws SerializerException
0939: */
0940: protected void processExport(String name, XMLBinding binding)
0941: throws SerializerException {
0942: this .logMe("*********collecting data*********");
0943: /** first create the snapshot file */
0944:
0945: setSnapshot(new JSSeedData(name));
0946:
0947: setSnapshotData();
0948:
0949: if (this
0950: .getSetting(JetspeedSerializer.KEY_PROCESS_CAPABILITIES)) {
0951: logMe("collecting clients, mediatypes and mimetypes");
0952: exportCapabilitiesInfrastructure();
0953: } else
0954: logMe("capabilities skipped");
0955:
0956: if (this .getSetting(JetspeedSerializer.KEY_PROCESS_USERS)) {
0957: logMe("collecting users");
0958: exportUsers();
0959: } else
0960: logMe("users skipped");
0961:
0962: if (this .getSetting(JetspeedSerializer.KEY_PROCESS_PROFILER)) {
0963: logMe("collecting profiling rules");
0964: this .getProfilingRules();
0965: } else
0966: logMe(" profiling rules skipped");
0967:
0968: if (this .getSetting(JetspeedSerializer.KEY_PROCESS_PERMISSIONS)) {
0969: logMe("collecting permissions");
0970: this .getPermissions();
0971: } else
0972: logMe(" permissions skipped");
0973:
0974: }
0975:
0976: /**
0977: * Setup the binding for the different classes, mapping each extracted class
0978: * to a unique tag name in the XML
0979: *
0980: * @param binding
0981: */
0982: protected void setupAliases(XMLBinding binding) {
0983: binding.setAlias(JSRole.class, "Role");
0984: binding.setAlias(JSRoles.class, "Roles");
0985: binding.setAlias(JSGroup.class, "Group");
0986: binding.setAlias(JSGroups.class, "Groups");
0987: binding.setAlias(JSUser.class, "User");
0988: binding.setAlias(JSUsers.class, "Users");
0989: binding.setAlias(JSNVPElements.class, "preferences");
0990: binding.setAlias(JSUserAttributes.class, "userinfo");
0991: binding.setAlias(JSSnapshot.class, "snapshot");
0992: binding.setAlias(JSUserRoles.class, "roles");
0993: binding.setAlias(JSUserGroups.class, "groups");
0994: binding.setAlias(JSClient.class, "Client");
0995: binding.setAlias(JSClients.class, "Clients");
0996: binding.setAlias(JSClientCapabilities.class, "capabilities");
0997: binding.setAlias(JSClientMimeTypes.class, "mimeTypes");
0998: binding.setAlias(JSMimeTypes.class, "MimeTypes");
0999: binding.setAlias(JSMimeType.class, "MimeType");
1000: binding.setAlias(JSCapabilities.class, "Capabilities");
1001: binding.setAlias(JSCapability.class, "Capability");
1002: binding.setAlias(JSMediaTypes.class, "MediaTypes");
1003: binding.setAlias(JSMediaType.class, "MediaType");
1004: binding.setAlias(JSUserUsers.class, "users");
1005:
1006: binding.setAlias(JSPermissions.class, "Permissions");
1007: binding.setAlias(JSPermission.class, "Permission");
1008: binding.setAlias(JSProfilingRules.class, "ProfilingRules");
1009: binding.setAlias(JSProfilingRule.class, "ProfilingRule");
1010: binding.setAlias(JSRuleCriterions.class, "Criteria");
1011: binding.setAlias(JSRuleCriterion.class, "Criterion");
1012:
1013: binding.setAlias(JSPrincipalRule.class, "Rule");
1014: binding.setAlias(JSPrincipalRules.class, "Rules");
1015:
1016: binding.setAlias(String.class, "String");
1017: binding.setAlias(Integer.class, "int");
1018:
1019: binding.setAlias(JSPWAttributes.class, "credentials");
1020:
1021: binding.setClassAttribute(null);
1022:
1023: }
1024:
1025: /**
1026: * simple lookup for principal object from a map
1027: * @param map
1028: * @param _fullPath
1029: * @return
1030: */
1031:
1032: private Object getObjectBehindPrinicpal(Map map,
1033: BasePrincipal _principal) {
1034: return getObjectBehindPath(map, _principal.getFullPath());
1035: }
1036:
1037: /**
1038: * create a serializable wrapper for role
1039: *
1040: * @param role
1041: * @return
1042: */
1043: private JSRole createJSRole(Role role) {
1044: JSRole _role = new JSRole();
1045: _role.setName(role.getPrincipal().getName());
1046: return _role;
1047: }
1048:
1049: /**
1050: * export roles
1051: *
1052: * @return
1053: */
1054: private void exportRoles() throws SerializerException {
1055: RoleManager roleManager = (RoleManager) getCM().getComponent(
1056: "org.apache.jetspeed.security.RoleManager");
1057: if (roleManager == null)
1058: throw new SerializerException(
1059: SerializerException.COMPONENTMANAGER_DOES_NOT_EXIST
1060: .create("org.apache.jetspeed.security.RoleManager"));
1061:
1062: Iterator list = null;
1063: try {
1064: list = roleManager.getRoles("");
1065: } catch (Exception e) {
1066: throw new SerializerException(
1067: SerializerException.GET_EXISTING_OBJECTS
1068: .create(new String[] { "Role",
1069: e.getMessage() }));
1070: }
1071: while (list.hasNext()) {
1072: try {
1073: Role role = (Role) list.next();
1074: JSRole _tempRole = (JSRole) getObjectBehindPrinicpal(
1075: roleMap, (BasePrincipal) (role.getPrincipal()));
1076: if (_tempRole == null) {
1077: _tempRole = createJSRole(role);
1078: roleMap.put(_tempRole.getName(), _tempRole);
1079: ((JSSeedData) getSnapshot()).getRoles().add(
1080: _tempRole);
1081: }
1082:
1083: } catch (Exception e) {
1084: throw new SerializerException(
1085: SerializerException.CREATE_SERIALIZED_OBJECT_FAILED
1086: .create(new String[] { "Role",
1087: e.getMessage() }));
1088: }
1089: }
1090: return;
1091: }
1092:
1093: /** Groups -------------------------- */
1094: /**
1095: * create a wrapper JSGroup object
1096: */
1097: private JSGroup createJSGroup(Group group) {
1098: JSGroup _group = new JSGroup();
1099: _group.setName(group.getPrincipal().getName());
1100: return _group;
1101: }
1102:
1103: /**
1104: * extract the groups from the current environment
1105: *
1106: * @throws SerializerException
1107: */
1108: private void exportGroups() throws SerializerException {
1109: GroupManager groupManager = (GroupManager) getCM()
1110: .getComponent(
1111: "org.apache.jetspeed.security.GroupManager");
1112: if (groupManager == null)
1113: throw new SerializerException(
1114: SerializerException.COMPONENTMANAGER_DOES_NOT_EXIST
1115: .create("org.apache.jetspeed.security.GroupManager"));
1116: Iterator list = null;
1117: try {
1118: list = groupManager.getGroups("");
1119: } catch (Exception e) {
1120: throw new SerializerException(
1121: SerializerException.GET_EXISTING_OBJECTS
1122: .create(new String[] { "Group",
1123: e.getMessage() }));
1124: }
1125: while (list.hasNext()) {
1126:
1127: try {
1128: Group group = (Group) list.next();
1129: JSGroup _tempGroup = (JSGroup) getObjectBehindPrinicpal(
1130: groupMap,
1131: (BasePrincipal) (group.getPrincipal()));
1132: if (_tempGroup == null) {
1133: _tempGroup = createJSGroup(group);
1134: groupMap.put(_tempGroup.getName(), _tempGroup);
1135: ((JSSeedData) getSnapshot()).getGroups().add(
1136: _tempGroup);
1137: }
1138:
1139: } catch (Exception e) {
1140: throw new SerializerException(
1141: SerializerException.CREATE_SERIALIZED_OBJECT_FAILED
1142: .create(new String[] { "Group",
1143: e.getMessage() }));
1144: }
1145: }
1146: return;
1147: }
1148:
1149: /**
1150: * Add the credentials to the JSUser object.
1151: * <p>
1152: * If the credential provided is a PasswordCredential, userid and password
1153: * are extracted and set explcitely
1154: *
1155: * @param isPublic
1156: * public or private credential
1157: * @param newUser
1158: * the JS user object reference
1159: * @param credential
1160: * the credential object
1161: */
1162:
1163: private void addJSUserCredentials(boolean isPublic, JSUser newUser,
1164: Object credential) {
1165: if (credential == null)
1166: return;
1167: if (credential instanceof PasswordCredential) {
1168: PasswordCredential pw = (PasswordCredential) credential;
1169: newUser
1170: .setUserCredential(pw.getUserName(), pw
1171: .getPassword(), pw.getExpirationDate(), pw
1172: .isEnabled(), pw.isExpired(), pw
1173: .isUpdateRequired());
1174: return;
1175: } else if (isPublic)
1176: newUser.addPublicCredential(credential);
1177: else
1178: newUser.addPrivateCredential(credential);
1179: }
1180:
1181: /**
1182: * create a new JSUser object
1183: *
1184: * @param user
1185: * @return a new JSUser object
1186: */
1187: private JSUser createJSUser(User user) {
1188: JSUser _newUser = new JSUser();
1189:
1190: Subject subject = user.getSubject();
1191: // get the user principal
1192: Set principals = subject.getPrincipals();
1193: Iterator list = principals.iterator();
1194: while (list.hasNext()) {
1195: BasePrincipal principal = (BasePrincipal) list.next();
1196: String path = principal.getFullPath();
1197: if (path.startsWith("/role/")) {
1198: JSRole _tempRole = (JSRole) this .getObjectBehindPath(
1199: roleMap, principal.getName());
1200: if (_tempRole != null) {
1201: _newUser.addRole(_tempRole);
1202: }
1203:
1204: } else {
1205: if (path.startsWith("/group/")) {
1206: JSGroup _tempGroup = (JSGroup) this
1207: .getObjectBehindPath(groupMap, principal
1208: .getName());
1209: if (_tempGroup != null) {
1210: _newUser.addGroup(_tempGroup);
1211: }
1212:
1213: } else if (path.startsWith("/user/"))
1214: _newUser.setPrincipal(principal);
1215:
1216: }
1217:
1218: }
1219: // System.out.println("User Public Credentials");
1220: Set credentials = subject.getPublicCredentials();
1221: list = credentials.iterator();
1222: while (list.hasNext()) {
1223: Object credential = list.next();
1224: addJSUserCredentials(true, _newUser, credential);
1225: }
1226: // System.out.println("User Private Credentials");
1227: credentials = subject.getPrivateCredentials();
1228: list = credentials.iterator();
1229: while (list.hasNext()) {
1230: Object credential = list.next();
1231: addJSUserCredentials(false, _newUser, credential);
1232: }
1233:
1234: Preferences preferences = user.getPreferences();
1235: _newUser.setPreferences(preferences);
1236: preferences = user.getUserAttributes();
1237: _newUser.setUserInfo(preferences);
1238: //TODO: HJB, fix preferences...userinfo doesn't return values in prefs_property_value (in fact preferences.keys() is []
1239: return _newUser;
1240: }
1241:
1242: /**
1243: * Collect all the roles, groups and users from the current environment.
1244: * Include the current SecurityProvider to understand, whether the password
1245: * collected can be used upon import
1246: *
1247: * @throws SerializerException
1248: */
1249:
1250: private void exportUsers() throws SerializerException {
1251: /** set the security provider info in the snapshot file */
1252: ((JSSeedData) getSnapshot())
1253: .setEncryption(getEncryptionString());
1254: /** get the roles */
1255: exportRoles();
1256: /** get the groups */
1257: exportGroups();
1258:
1259: /** users */
1260: UserManager userManager = (UserManager) getCM().getComponent(
1261: "org.apache.jetspeed.security.UserManager");
1262: if (userManager == null)
1263: throw new SerializerException(
1264: SerializerException.COMPONENTMANAGER_DOES_NOT_EXIST
1265: .create("org.apache.jetspeed.security.UserManager"));
1266: Iterator list = null;
1267: try {
1268: list = userManager.getUsers("");
1269: } catch (Exception e) {
1270: throw new SerializerException(
1271: SerializerException.GET_EXISTING_OBJECTS
1272: .create(new String[] { "User",
1273: e.getMessage() }));
1274: }
1275: while (list.hasNext()) {
1276:
1277: try {
1278: User _user = (User) list.next();
1279: JSUser _tempUser = createJSUser(_user);
1280: userMap.put(_tempUser.getName(), _tempUser);
1281: ((JSSeedData) getSnapshot()).getUsers().add(_tempUser);
1282: } catch (Exception e) {
1283: throw new SerializerException(
1284: SerializerException.CREATE_SERIALIZED_OBJECT_FAILED
1285: .create(new String[] { "User",
1286: e.getMessage() }));
1287: }
1288:
1289: }
1290: return;
1291:
1292: }
1293:
1294: /**
1295: * extract the capabilities and save in snapshot file
1296: *
1297: * @param caps
1298: * capability-container
1299: * @throws SerializerException
1300: */
1301: private void exportCapabilites(Capabilities caps)
1302: throws SerializerException {
1303:
1304: Iterator list = caps.getCapabilities();
1305:
1306: while (list.hasNext()) {
1307: try {
1308: Capability _cp = (Capability) list.next();
1309: JSCapability _jsC = new JSCapability();
1310: _jsC.setName(_cp.getName());
1311: this .capabilityMap.put(_jsC.getName(), _jsC);
1312: this .capabilityMapInt.put(new Integer(_cp
1313: .getCapabilityId()), _jsC);
1314: ((JSSeedData) getSnapshot()).getCapabilities()
1315: .add(_jsC);
1316: } catch (Exception e) {
1317: throw new SerializerException(
1318: SerializerException.CREATE_SERIALIZED_OBJECT_FAILED
1319: .create(new String[] { "C",
1320: e.getMessage() }));
1321: }
1322: }
1323: return;
1324:
1325: }
1326:
1327: /**
1328: * exstract the mime types anmd save in snapshot file
1329: *
1330: * @param caps
1331: * capability container
1332: * @throws SerializerException
1333: */
1334: private void exportMimeTypes(Capabilities caps)
1335: throws SerializerException {
1336:
1337: Iterator list = caps.getMimeTypes();
1338:
1339: while (list.hasNext()) {
1340: try {
1341: MimeType _mt = (MimeType) list.next();
1342: JSMimeType _jsM = new JSMimeType();
1343: _jsM.setName(_mt.getName());
1344: this .mimeMap.put(_jsM.getName(), _jsM);
1345: this .mimeMapInt.put(new Integer(_mt.getMimetypeId()),
1346: _jsM);
1347:
1348: ((JSSeedData) getSnapshot()).getMimeTypes().add(_jsM);
1349: } catch (Exception e) {
1350: throw new SerializerException(
1351: SerializerException.CREATE_SERIALIZED_OBJECT_FAILED
1352: .create(new String[] { "MimeType",
1353: e.getMessage() }));
1354: }
1355: }
1356: return;
1357:
1358: }
1359:
1360: /**
1361: * create a JS CLient
1362: *
1363: * @param c
1364: * the existing Client object
1365: * @return
1366: * @throws SerializerException
1367: */
1368:
1369: private JSClient createJSClient(Client c)
1370: throws SerializerException {
1371: try {
1372: JSClient jsC = new JSClient(c);
1373: // find the mimeTypes
1374: Iterator _itM = c.getMimetypes().iterator();
1375: while (_itM.hasNext()) {
1376: MimeType _m = (MimeType) _itM.next();
1377: JSMimeType _mt = (JSMimeType) mimeMap.get(_m.getName());
1378: if (_mt != null)
1379: jsC.getMimeTypes().add(_mt);
1380: }
1381:
1382: Integer id = new Integer(c.getPreferredMimeTypeId());
1383: JSMimeType _mt = (JSMimeType) mimeMapInt.get(id);
1384: if (_mt != null)
1385: jsC.setPreferredMimeTypeID(_mt.getName());
1386: else
1387: jsC.setPreferredMimeTypeID("???");
1388:
1389: // find the capabilities
1390: Iterator _itC = c.getCapabilities().iterator();
1391: while (_itC.hasNext()) {
1392: Capability _c = (Capability) _itC.next();
1393: JSCapability _ct = (JSCapability) capabilityMap.get(_c
1394: .getName());
1395: if (_ct != null)
1396: jsC.getCapabilities().add(_ct);
1397: }
1398:
1399: return jsC;
1400: } catch (Exception e) {
1401: throw new SerializerException(
1402: SerializerException.CREATE_SERIALIZED_OBJECT_FAILED
1403: .create(new String[] { "Client",
1404: e.getMessage() }));
1405: }
1406:
1407: }
1408:
1409: /**
1410: * extract the current clients and save in the snapshot file
1411: *
1412: * @param list
1413: * @param caps
1414: * @return
1415: * @throws SerializerException
1416: */
1417: private void exportClients(Capabilities caps)
1418: throws SerializerException {
1419:
1420: /** first the the mime types */
1421: exportMimeTypes(caps);
1422:
1423: /** second get the capabilities */
1424: this .exportCapabilites(caps);
1425:
1426: /** now get the clients */
1427: Iterator _it = caps.getClients();
1428: while (_it.hasNext()) {
1429: Client c = (Client) _it.next();
1430: JSClient jsC = createJSClient(c);
1431: if (jsC == null)
1432: throw new SerializerException(
1433: SerializerException.CREATE_SERIALIZED_OBJECT_FAILED
1434: .create(new String[] { "Client",
1435: "createClient returned NULL" }));
1436: this .clientMap.put(jsC.getName(), jsC);
1437: ((JSSeedData) getSnapshot()).getClients().add(jsC);
1438: }
1439: return;
1440: }
1441:
1442: /**
1443: * extract the media types and save in snapshot file
1444: *
1445: * @param caps
1446: * capabilit container
1447: * @throws SerializerException
1448: */
1449: private void exportMediaTypes(Capabilities caps)
1450: throws SerializerException {
1451: Iterator list = caps.getMediaTypes();
1452:
1453: while (list.hasNext()) {
1454: try {
1455: MediaType _mt = (MediaType) list.next();
1456: JSMediaType _jsM = new JSMediaType(_mt);
1457: // find the mimeTypes
1458: Iterator _itM = _mt.getMimetypes().iterator();
1459: while (_itM.hasNext()) {
1460: MimeType _m = (MimeType) _itM.next();
1461: JSMimeType _mttype = (JSMimeType) mimeMap.get(_m
1462: .getName());
1463: if (_mttype != null)
1464: _jsM.getMimeTypes().add(_mttype);
1465: }
1466: // find the capabilities
1467: Iterator _itC = _mt.getCapabilities().iterator();
1468: while (_itC.hasNext()) {
1469: Capability _c = (Capability) _itC.next();
1470: JSCapability _ct = (JSCapability) capabilityMap
1471: .get(_c.getName());
1472: if (_ct != null)
1473: _jsM.getCapabilities().add(_ct);
1474: }
1475: this .mediaMap.put(_jsM.getName(), _jsM);
1476: ((JSSeedData) getSnapshot()).getMediaTypes().add(_jsM);
1477: } catch (Exception e) {
1478: // do whatever
1479: throw new SerializerException(
1480: SerializerException.CREATE_SERIALIZED_OBJECT_FAILED
1481: .create(new String[] { "MediaType",
1482: e.getMessage() }));
1483: }
1484: }
1485: return;
1486: }
1487:
1488: /**
1489: * Extracts all capability related objects (clients, mimetypes and
1490: * mediatypes) and their relationships
1491: *
1492: * @return
1493: */
1494:
1495: private void exportCapabilitiesInfrastructure()
1496: throws SerializerException {
1497: Capabilities caps = (Capabilities) getCM().getComponent(
1498: "org.apache.jetspeed.capabilities.Capabilities");
1499: if (caps == null)
1500: throw new SerializerException(
1501: SerializerException.COMPONENTMANAGER_DOES_NOT_EXIST
1502: .create("org.apache.jetspeed.capabilities.Capabilities"));
1503:
1504: /**
1505: * get the clients (which in turn will get the mime types and
1506: * capailities)
1507: */
1508:
1509: exportClients(caps);
1510: // get the mediatTypes, too
1511:
1512: exportMediaTypes(caps);
1513:
1514: }
1515:
1516: /**
1517: * extract all permissions from the current environment
1518: *
1519: * @throws SerializerException
1520: */
1521: private void getPermissions() throws SerializerException {
1522: Object o = null;
1523: PermissionManager pm = (PermissionManager) getCM()
1524: .getComponent(
1525: "org.apache.jetspeed.security.PermissionManager");
1526: if (pm == null)
1527: throw new SerializerException(
1528: SerializerException.COMPONENTMANAGER_DOES_NOT_EXIST
1529: .create("org.apache.jetspeed.security.PermissionManager"));
1530:
1531: Iterator list = null;
1532: try {
1533: list = pm.getPermissions().iterator();
1534: } catch (Exception e) {
1535: throw new SerializerException(
1536: SerializerException.GET_EXISTING_OBJECTS
1537: .create(new String[] { "Permissions",
1538: e.getMessage() }));
1539: }
1540:
1541: while (list.hasNext()) {
1542: try {
1543: JSPermission _js = new JSPermission();
1544:
1545: InternalPermission p = (InternalPermission) list.next();
1546: _js.setResource(p.getName());
1547: _js.setActions(p.getActions());
1548: _js.setId(p.getPermissionId());
1549: _js.setType(_js.getTypeForClass(p.getClassname()));
1550:
1551: Iterator list2 = p.getPrincipals().iterator();
1552: while (list2.hasNext()) {
1553: o = list2.next();
1554: InternalPrincipal principal = (InternalPrincipal) o;
1555: String path = principal.getFullPath();
1556: if (path.startsWith("/role/")) {
1557: JSRole _tempRole = (JSRole) this
1558: .getObjectBehindPath(
1559: roleMap,
1560: removeFromString(path, "/role/"));
1561: if (_tempRole != null) {
1562: _js.addRole(_tempRole);
1563: }
1564:
1565: } else {
1566: if (path.startsWith("/group/")) {
1567: JSGroup _tempGroup = (JSGroup) this
1568: .getObjectBehindPath(groupMap,
1569: removeFromString(path,
1570: "/group/"));
1571: if (_tempGroup != null) {
1572: _js.addGroup(_tempGroup);
1573: }
1574:
1575: } else {
1576: if (path.startsWith("/user/")) {
1577: JSUser _tempUser = (JSUser) this
1578: .getObjectBehindPath(userMap,
1579: removeFromString(path,
1580: "/user/"));
1581: if (_tempUser != null) {
1582: _js.addUser(_tempUser);
1583: }
1584:
1585: }
1586:
1587: }
1588:
1589: }
1590: }
1591: this .permissionMap.put(_js.getType(), _js);
1592: ((JSSeedData) getSnapshot()).getPermissions().add(_js);
1593:
1594: } catch (Exception e) {
1595: throw new SerializerException(
1596: SerializerException.CREATE_SERIALIZED_OBJECT_FAILED
1597: .create(new String[] { "Permissions",
1598: e.getMessage() }));
1599: }
1600: }
1601: return;
1602:
1603: }
1604:
1605: /**
1606: * Create the Profiling Rule Wrapper
1607: *
1608: * @param p
1609: * @return
1610: */
1611: private JSProfilingRule createProfilingRule(ProfilingRule p,
1612: boolean standard) {
1613: JSProfilingRule rule = new JSProfilingRule();
1614:
1615: rule.setStandardRule(standard);
1616: rule.setDescription(p.getTitle());
1617: rule.setId(p.getId());
1618:
1619: Collection col = p.getRuleCriteria();
1620: Iterator keys = col.iterator();
1621: while (keys.hasNext()) {
1622: RuleCriterion rc = (RuleCriterion) keys.next();
1623: rule.getCriterions().add(new JSRuleCriterion(rc));
1624: }
1625: return rule;
1626:
1627: }
1628:
1629: /**
1630: * read the permissions and then the profiling rules.
1631: * <p>
1632: * after that update the cross reference with the users
1633: *
1634: * @throws SerializerException
1635: */
1636: private void getProfilingRules() throws SerializerException {
1637: Profiler pm = (Profiler) getCM().getComponent(
1638: "org.apache.jetspeed.profiler.Profiler");
1639: if (pm == null)
1640:
1641: throw new SerializerException(
1642: SerializerException.COMPONENTMANAGER_DOES_NOT_EXIST
1643: .create("org.apache.jetspeed.profiler.Profiler"));
1644: Class standardRuleClass = null;
1645: try {
1646: ProfilingRule tempStandardRule = pm
1647: .createProfilingRule(true);
1648: standardRuleClass = tempStandardRule.getClass();
1649: } catch (Exception e) {
1650: throw new SerializerException(
1651: SerializerException.CREATE_OBJECT_FAILED
1652: .create(new String[] { "Standard Rule",
1653: e.getMessage() }));
1654: }
1655:
1656: Iterator list = null;
1657: try {
1658: list = pm.getRules().iterator();
1659: } catch (Exception e) {
1660: throw new SerializerException(
1661: SerializerException.GET_EXISTING_OBJECTS
1662: .create(new String[] { "ProfilingRules",
1663: e.getMessage() }));
1664: }
1665: while (list.hasNext()) {
1666: try {
1667: ProfilingRule p = (ProfilingRule) list.next();
1668: if (!(this .rulesMap.containsKey(p.getId()))) {
1669: JSProfilingRule rule = createProfilingRule(p,
1670: (standardRuleClass == p.getClass()));
1671: rulesMap.put(rule.getId(), rule);
1672: ((JSSeedData) getSnapshot()).getRules().add(rule);
1673:
1674: }
1675: } catch (Exception e) {
1676: throw new SerializerException(
1677: SerializerException.CREATE_SERIALIZED_OBJECT_FAILED
1678: .create(new String[] {
1679: "ProfilingRules",
1680: e.getMessage() }));
1681: }
1682: }
1683:
1684: // determine the defualt rule
1685: ProfilingRule defaultRule = pm.getDefaultRule();
1686: if (defaultRule != null)
1687: ((JSSeedData) getSnapshot()).setDefaultRule(defaultRule
1688: .getId());
1689:
1690: // get Rules for each user
1691:
1692: Iterator _itUsers = this .userMap.values().iterator();
1693: while (_itUsers.hasNext()) {
1694: JSUser _user = (JSUser) _itUsers.next();
1695: Principal principal = _user.getPrincipal();
1696: if (principal != null) {
1697: Collection col = pm.getRulesForPrincipal(principal);
1698: Iterator _itCol = col.iterator();
1699: while (_itCol.hasNext()) {
1700: PrincipalRule p1 = (PrincipalRule) _itCol.next();
1701: JSPrincipalRule pr = new JSPrincipalRule(p1
1702: .getLocatorName(), p1.getProfilingRule()
1703: .getId());
1704: _user.getRules().add(pr);
1705: }
1706: }
1707: }
1708:
1709: return;
1710:
1711: }
1712:
1713: /**
1714: * Establish whether incoming passwords are "clear" text or whether they are
1715: * to be decoded. That however depends on whether the passwords were encoded
1716: * with the current active provider or not.
1717: *
1718: * @return
1719: */
1720: protected int compareCurrentSecurityProvider(JSSeedData file) {
1721: String _fileEncryption = file.getEncryption();
1722: if ((_fileEncryption == null)
1723: || (_fileEncryption.length() == 0))
1724: return NO_DECODING; // passwords are in clear text
1725:
1726: if (_fileEncryption.equals(getEncryptionString()))
1727: return PASSTHRU_REQUIRED;
1728: else
1729: return NO_DECODING;
1730: }
1731:
1732: private String getEncryptionString() {
1733: PasswordCredentialProvider provider = (PasswordCredentialProvider) getCM()
1734: .getComponent(
1735: "org.apache.jetspeed.security.spi.PasswordCredentialProvider");
1736: if (provider == null) {
1737: System.err
1738: .println("Error!!! PasswordCredentialProvider not available");
1739: return ENCODING_STRING;
1740: }
1741: try {
1742: PasswordCredential credential = provider.create(JETSPEED,
1743: ENCODING_STRING);
1744: if ((credential != null)
1745: && (credential.getPassword() != null))
1746: return new String(credential.getPassword());
1747: else
1748: return ENCODING_STRING;
1749: } catch (Exception e) {
1750: e.printStackTrace();
1751: return ENCODING_STRING;
1752: }
1753: }
1754:
1755: /**
1756: * ++++++++++++++++++++++++++++++HELPERS
1757: * +++++++++++++++++++++++++++++++++++++++++++++
1758: */
1759:
1760: /**
1761: * recreate a rule criterion object from the deserialized wrapper
1762: * @param profiler established profile manager
1763: * @param jsr deserialized object
1764: * @return new RuleCriterion with content set to deserialized wrapepr
1765: * @throws SerializerException
1766: */
1767: protected RuleCriterion recreateRuleCriterion(Profiler profiler,
1768: JSRuleCriterion jsr, ProfilingRule rule)
1769: throws SerializerException, ClassNotFoundException
1770:
1771: {
1772: try {
1773:
1774: RuleCriterion c = profiler.createRuleCriterion();
1775: if (c == null)
1776: throw new SerializerException(
1777: SerializerException.CREATE_OBJECT_FAILED
1778: .create(
1779: "org.apache.jetspeed.profiler.rules.RuleCriterion",
1780: "returned null"));
1781: c.setFallbackOrder(jsr.getFallBackOrder());
1782: c.setFallbackType(jsr.getFallBackType());
1783: c.setName(jsr.getName());
1784: c.setType(jsr.getType());
1785: c.setValue(jsr.getValue());
1786: c.setRuleId(rule.getId());
1787: return c;
1788: } catch (Exception e) {
1789: SerializerException.CREATE_OBJECT_FAILED.create(
1790: "org.apache.jetspeed.profiler.rules.RuleCriterion",
1791: e.getLocalizedMessage());
1792: return null;
1793: }
1794: }
1795:
1796: /**
1797: * recreate a profiling rule object from the deserialized wrapper and store it
1798: * @param profiler established profile manager
1799: * @param jsp deserialized object
1800: * @
1801: * @throws SerializerException, ClassNotFoundException, ProfilerException
1802: */
1803: protected ProfilingRule recreateRule(Profiler profiler,
1804: ProfilingRule existingRule, JSProfilingRule jsp)
1805: throws SerializerException, ClassNotFoundException,
1806: ProfilerException {
1807: ProfilingRule rule = null;
1808: boolean existing = false;
1809:
1810: if (existingRule == null) {
1811: rule = profiler.getRule(jsp.getId());
1812: if (jsp.isStandardRule())
1813: rule = profiler.createProfilingRule(true);
1814: else
1815: rule = profiler.createProfilingRule(false);
1816: rule.setId(jsp.getId());
1817: } else {
1818: rule = existingRule;
1819: existing = true;
1820: }
1821:
1822: rule.setTitle(jsp.getDescription());
1823:
1824: JSRuleCriterions col = jsp.getCriterions();
1825:
1826: Iterator _it = col.iterator();
1827: while (_it.hasNext()) {
1828: RuleCriterion c = recreateRuleCriterion(profiler,
1829: (JSRuleCriterion) _it.next(), rule);
1830: if (c != null) {
1831: Collection cHelp = rule.getRuleCriteria();
1832: if ((existing) && (cHelp.contains(c)))
1833: cHelp.remove(c); //remove existing duplicate
1834: cHelp.add(c); // add the current version back in
1835: }
1836: }
1837: return rule;
1838:
1839: }
1840:
1841: private void recreateProfilingRules() throws SerializerException {
1842: logMe("recreateProfilingRules - processing");
1843: Profiler pm = (Profiler) getCM().getComponent(
1844: "org.apache.jetspeed.profiler.Profiler");
1845: if (pm == null)
1846: throw new SerializerException(
1847: SerializerException.COMPONENTMANAGER_DOES_NOT_EXIST
1848: .create("org.apache.jetspeed.profiler.Profiler"));
1849: JSProfilingRules rules = ((JSSeedData) getSnapshot())
1850: .getRules();
1851: if ((rules != null) && (rules.size() > 0)) {
1852: Iterator _it = rules.iterator();
1853: while (_it.hasNext()) {
1854: JSProfilingRule _c = (JSProfilingRule) _it.next();
1855:
1856: try {
1857: ProfilingRule rule = null;
1858:
1859: rule = pm.getRule(_c.getId());
1860: if ((rule == null)
1861: || (this
1862: .getSetting(JetspeedSerializer.KEY_OVERWRITE_EXISTING))) {
1863: rule = recreateRule(pm, rule, _c);
1864: pm.storeProfilingRule(rule);
1865: }
1866: } catch (Exception e) {
1867: throw new SerializerException(
1868: SerializerException.CREATE_OBJECT_FAILED
1869: .create(
1870: "org.apache.jetspeed.capabilities.Capabilities",
1871: e.getLocalizedMessage()));
1872: }
1873: }
1874: /** reset the default profiling rule */
1875: String defaultRuleID = ((JSSeedData) getSnapshot())
1876: .getDefaultRule();
1877: if (defaultRuleID != null) {
1878: ProfilingRule defaultRule = pm.getRule(defaultRuleID);
1879: if (defaultRule != null)
1880: pm.setDefaultRule(defaultRuleID);
1881: }
1882: } else
1883: logMe("NO PROFILING RULES?????");
1884: logMe("recreateProfilingRules - done");
1885: }
1886:
1887: }
|