diff options
author | Eike Stepper | 2012-06-12 09:39:49 +0000 |
---|---|---|
committer | Eike Stepper | 2012-06-12 09:39:49 +0000 |
commit | bd3afd74c3edb5dbef2a1b9c31f0e7b985b36872 (patch) | |
tree | cc78cfe4d7bf2d92483799b1e7cc4f14908b395f | |
parent | c1eff50f896c1dfb9cce4e4e7366c2a3d57481ba (diff) | |
download | cdo-bd3afd74c3edb5dbef2a1b9c31f0e7b985b36872.tar.gz cdo-bd3afd74c3edb5dbef2a1b9c31f0e7b985b36872.tar.xz cdo-bd3afd74c3edb5dbef2a1b9c31f0e7b985b36872.zip |
Support BigInteger and BigDecimal in export/import
2 files changed, 1402 insertions, 1378 deletions
diff --git a/plugins/org.eclipse.emf.cdo.server/src/org/eclipse/emf/cdo/server/CDOServerExporter.java b/plugins/org.eclipse.emf.cdo.server/src/org/eclipse/emf/cdo/server/CDOServerExporter.java index 6d03f8f3ca..abc921ee7c 100644 --- a/plugins/org.eclipse.emf.cdo.server/src/org/eclipse/emf/cdo/server/CDOServerExporter.java +++ b/plugins/org.eclipse.emf.cdo.server/src/org/eclipse/emf/cdo/server/CDOServerExporter.java @@ -1,705 +1,717 @@ -/*
- * Copyright (c) 2004 - 2012 Eike Stepper (Berlin, Germany) and others.
- * All rights reserved. This program and the accompanying materials
- * are made available under the terms of the Eclipse Public License v1.0
- * which accompanies this distribution, and is available at
- * http://www.eclipse.org/legal/epl-v10.html
- *
- * Contributors:
- * Eike Stepper - initial API and implementation
- */
-package org.eclipse.emf.cdo.server;
-
-import org.eclipse.emf.cdo.common.branch.CDOBranch;
-import org.eclipse.emf.cdo.common.branch.CDOBranchHandler;
-import org.eclipse.emf.cdo.common.branch.CDOBranchPoint;
-import org.eclipse.emf.cdo.common.commit.CDOCommitInfo;
-import org.eclipse.emf.cdo.common.commit.CDOCommitInfoHandler;
-import org.eclipse.emf.cdo.common.id.CDOID;
-import org.eclipse.emf.cdo.common.id.CDOIDUtil;
-import org.eclipse.emf.cdo.common.lob.CDOBlob;
-import org.eclipse.emf.cdo.common.lob.CDOClob;
-import org.eclipse.emf.cdo.common.lob.CDOLobHandler;
-import org.eclipse.emf.cdo.common.model.CDOClassInfo;
-import org.eclipse.emf.cdo.common.model.CDOClassifierRef;
-import org.eclipse.emf.cdo.common.model.CDOPackageUnit;
-import org.eclipse.emf.cdo.common.model.EMFUtil;
-import org.eclipse.emf.cdo.common.revision.CDORevision;
-import org.eclipse.emf.cdo.common.revision.CDORevisionHandler;
-import org.eclipse.emf.cdo.spi.common.branch.InternalCDOBranchManager;
-import org.eclipse.emf.cdo.spi.common.commit.InternalCDOCommitInfoManager;
-import org.eclipse.emf.cdo.spi.common.model.InternalCDOPackageInfo;
-import org.eclipse.emf.cdo.spi.common.model.InternalCDOPackageRegistry;
-import org.eclipse.emf.cdo.spi.common.model.InternalCDOPackageUnit;
-import org.eclipse.emf.cdo.spi.common.revision.InternalCDORevision;
-import org.eclipse.emf.cdo.spi.server.InternalRepository;
-import org.eclipse.emf.cdo.spi.server.InternalSession;
-
-import org.eclipse.net4j.util.HexUtil;
-import org.eclipse.net4j.util.WrappedException;
-import org.eclipse.net4j.util.io.XMLOutput;
-import org.eclipse.net4j.util.lifecycle.LifecycleUtil;
-
-import org.eclipse.emf.ecore.EPackage;
-import org.eclipse.emf.ecore.EStructuralFeature;
-import org.eclipse.emf.ecore.util.FeatureMap;
-import org.eclipse.emf.ecore.util.FeatureMapUtil;
-
-import org.xml.sax.SAXException;
-
-import java.io.OutputStream;
-import java.io.Writer;
-import java.util.Date;
-import java.util.List;
-
-/**
- * Exports the complete contents of a {@link IRepository repository} in a format suitable for {@link CDOServerImporter
- * imports} into new repositories.
- * <p>
- * Subtypes specifiy the actual exchange format.
- *
- * @author Eike Stepper
- * @since 4.0
- */
-public abstract class CDOServerExporter<OUT>
-{
- private InternalRepository repository;
-
- public CDOServerExporter(IRepository repository)
- {
- this.repository = (InternalRepository)repository;
- }
-
- public final IRepository getRepository()
- {
- return repository;
- }
-
- public final void exportRepository(OutputStream out) throws Exception
- {
- boolean wasActive = LifecycleUtil.isActive(repository);
- if (!wasActive)
- {
- LifecycleUtil.activate(repository);
- }
-
- InternalSession session = repository.getSessionManager().openSession(null);
- StoreThreadLocal.setSession(session);
-
- try
- {
- OUT output = createOutput(out);
- exportAll(output);
- }
- finally
- {
- StoreThreadLocal.release();
- if (!wasActive)
- {
- LifecycleUtil.deactivate(repository);
- }
-
- repository = null;
- }
- }
-
- protected abstract OUT createOutput(OutputStream out) throws Exception;
-
- protected void exportAll(final OUT out) throws Exception
- {
- try
- {
- exportPackages(out);
- exportBranches(out);
- exportLobs(out);
- exportCommits(out);
- }
- catch (WrappedException ex)
- {
- throw WrappedException.unwrap(ex);
- }
- }
-
- protected void exportPackages(OUT out) throws Exception
- {
- InternalCDOPackageRegistry packageRegistry = repository.getPackageRegistry(false);
- InternalCDOPackageUnit[] packageUnits = packageRegistry.getPackageUnits(false);
- for (InternalCDOPackageUnit packageUnit : packageUnits)
- {
- String id = packageUnit.getID();
- CDOPackageUnit.Type type = packageUnit.getOriginalType();
- long time = packageUnit.getTimeStamp();
-
- EPackage ePackage = packageUnit.getTopLevelPackageInfo().getEPackage();
- String data = new String(EMFUtil.getEPackageBytes(ePackage, false, packageRegistry));
-
- startPackageUnit(out, id, type, time, data);
- for (InternalCDOPackageInfo packageInfo : packageUnit.getPackageInfos())
- {
- String packageURI = packageInfo.getPackageURI();
- exportPackageInfo(out, packageURI);
- }
-
- endPackageUnit(out);
- }
- }
-
- protected abstract void startPackageUnit(OUT out, String id, CDOPackageUnit.Type type, long time, String data)
- throws Exception;
-
- protected abstract void endPackageUnit(OUT out) throws Exception;
-
- protected abstract void exportPackageInfo(OUT out, String packageURI) throws Exception;
-
- protected void exportBranches(final OUT out) throws Exception
- {
- InternalCDOBranchManager branchManager = repository.getBranchManager();
- exportBranch(out, branchManager.getMainBranch());
-
- if (repository.isSupportingBranches())
- {
- branchManager.getBranches(0, 0, new CDOBranchHandler()
- {
- public void handleBranch(CDOBranch branch)
- {
- try
- {
- exportBranch(out, branch);
- }
- catch (Exception ex)
- {
- throw WrappedException.wrap(ex);
- }
- }
- });
- }
- }
-
- protected void exportBranch(OUT out, CDOBranch branch) throws Exception
- {
- exportRevisions(out, branch);
- }
-
- protected void exportRevisions(final OUT out, CDOBranch branch) throws Exception
- {
- repository.handleRevisions(null, branch, true, CDOBranchPoint.INVALID_DATE, false,
- new CDORevisionHandler.Filtered.Undetached(new CDORevisionHandler()
- {
- public boolean handleRevision(CDORevision revision)
- {
- try
- {
- exportRevision(out, revision);
- return true;
- }
- catch (Exception ex)
- {
- throw WrappedException.wrap(ex);
- }
- }
- }));
- }
-
- protected abstract void exportRevision(OUT out, CDORevision revision) throws Exception;
-
- protected void exportLobs(final OUT out) throws Exception
- {
- repository.handleLobs(0, 0, new CDOLobHandler()
- {
- public OutputStream handleBlob(byte[] id, long size)
- {
- try
- {
- return startBlob(out, id, size);
- }
- catch (Exception ex)
- {
- throw WrappedException.wrap(ex);
- }
- }
-
- public Writer handleClob(byte[] id, long size)
- {
- try
- {
- return startClob(out, id, size);
- }
- catch (Exception ex)
- {
- throw WrappedException.wrap(ex);
- }
- }
- });
- }
-
- protected abstract OutputStream startBlob(OUT out, byte[] id, long size) throws Exception;
-
- protected abstract Writer startClob(OUT out, byte[] id, long size) throws Exception;
-
- protected void exportCommits(final OUT out) throws Exception
- {
- InternalCDOCommitInfoManager commitInfoManager = repository.getCommitInfoManager();
- commitInfoManager.getCommitInfos(null, 0L, 0L, new CDOCommitInfoHandler()
- {
- public void handleCommitInfo(CDOCommitInfo commitInfo)
- {
- try
- {
- exportCommit(out, commitInfo);
- }
- catch (Exception ex)
- {
- throw WrappedException.wrap(ex);
- }
- }
- });
- }
-
- protected abstract void exportCommit(OUT out, CDOCommitInfo commitInfo) throws Exception;
-
- /**
- * XML constants being used by both {@link CDOServerExporter exporters} and {@link CDOServerImporter importers}.
- *
- * @author Eike Stepper
- */
- public static interface XMLConstants
- {
- public static final String REPOSITORY = "repository";
-
- public static final String REPOSITORY_NAME = "name";
-
- public static final String REPOSITORY_UUID = "uuid";
-
- public static final String REPOSITORY_ROOT = "root";
-
- public static final String REPOSITORY_CREATED = "created";
-
- public static final String REPOSITORY_COMMITTED = "committed";
-
- public static final String MODELS = "models";
-
- public static final String PACKAGE_UNIT = "packageUnit";
-
- public static final String PACKAGE_UNIT_ID = "id";
-
- public static final String PACKAGE_UNIT_TYPE = "type";
-
- public static final String PACKAGE_UNIT_TIME = "time";
-
- public static final String PACKAGE_UNIT_DATA = "data";
-
- public static final String PACKAGE_INFO = "packageInfo";
-
- public static final String PACKAGE_INFO_URI = "uri";
-
- public static final String INSTANCES = "instances";
-
- public static final String BRANCH = "branch";
-
- public static final String BRANCH_ID = "id";
-
- public static final String BRANCH_NAME = "name";
-
- public static final String BRANCH_TIME = "time";
-
- public static final String BRANCH_PARENT = "parent";
-
- public static final String REVISION = "revision";
-
- public static final String REVISION_ID = "id";
-
- public static final String REVISION_CLASS = "class";
-
- public static final String REVISION_VERSION = "version";
-
- public static final String REVISION_TIME = "time";
-
- public static final String REVISION_REVISED = "revised";
-
- public static final String REVISION_RESOURCE = "resource";
-
- public static final String REVISION_CONTAINER = "container";
-
- public static final String REVISION_FEATURE = "feature";
-
- public static final String FEATURE = "feature";
-
- public static final String FEATURE_NAME = "name";
-
- public static final String FEATURE_TYPE = "type";
-
- public static final String FEATURE_INNER_FEATURE = "innerFeature";
-
- public static final String FEATURE_INNER_TYPE = "innerType";
-
- public static final String FEATURE_VALUE = "value";
-
- public static final String FEATURE_ID = "id";
-
- public static final String FEATURE_SIZE = "size";
-
- public static final String TYPE_BLOB = "Blob";
-
- public static final String TYPE_CLOB = "Clob";
-
- public static final String TYPE_FEATURE_MAP = "FeatureMap";
-
- public static final String LOBS = "lobs";
-
- public static final String LOB_ID = "id";
-
- public static final String LOB_SIZE = "size";
-
- public static final String BLOB = "blob";
-
- public static final String CLOB = "clob";
-
- public static final String COMMITS = "commits";
-
- public static final String COMMIT = "commit";
-
- public static final String COMMIT_TIME = "time";
-
- public static final String COMMIT_PREVIOUS = "previous";
-
- public static final String COMMIT_BRANCH = "branch";
-
- public static final String COMMIT_USER = "user";
-
- public static final String COMMIT_COMMENT = "comment";
- }
-
- /**
- * An {@link CDOServerExporter exporter} that creates XML output suitable to be interpreted by an
- * {@link CDOServerImporter.XML XML importer}.
- *
- * @author Eike Stepper
- */
- public static class XML extends CDOServerExporter<XMLOutput> implements XMLConstants
- {
- public XML(IRepository repository)
- {
- super(repository);
- }
-
- @Override
- protected final XMLOutput createOutput(OutputStream out) throws Exception
- {
- return new XMLOutput(out);
- }
-
- @Override
- protected void exportAll(XMLOutput out) throws Exception
- {
- out.element(REPOSITORY);
- out.attribute(REPOSITORY_NAME, getRepository().getName());
- out.attribute(REPOSITORY_UUID, getRepository().getUUID());
- out.attribute(REPOSITORY_ROOT, str(getRepository().getRootResourceID()));
- out.attribute(REPOSITORY_CREATED, getRepository().getStore().getCreationTime());
- out.attribute(REPOSITORY_COMMITTED, getRepository().getLastCommitTimeStamp());
-
- out.push();
- super.exportAll(out);
- out.done();
- }
-
- @Override
- protected void exportPackages(XMLOutput out) throws Exception
- {
- out.element(MODELS);
-
- out.push();
- super.exportPackages(out);
- out.pop();
- }
-
- @Override
- protected void startPackageUnit(XMLOutput out, String id, CDOPackageUnit.Type type, long time, String data)
- throws Exception
- {
- out.element(PACKAGE_UNIT);
- out.attribute(PACKAGE_UNIT_ID, id);
- out.attribute(PACKAGE_UNIT_TYPE, type);
- out.attribute(PACKAGE_UNIT_TIME, time);
- out.attribute(PACKAGE_UNIT_DATA, data);
- out.push();
- }
-
- @Override
- protected void endPackageUnit(XMLOutput out) throws Exception
- {
- out.pop();
- }
-
- @Override
- protected void exportPackageInfo(XMLOutput out, String uri) throws Exception
- {
- out.element(PACKAGE_INFO);
- out.attribute(PACKAGE_INFO_URI, uri);
- }
-
- @Override
- protected void exportBranches(XMLOutput out) throws Exception
- {
- out.element(INSTANCES);
-
- out.push();
- super.exportBranches(out);
- out.pop();
- }
-
- @Override
- protected void exportBranch(XMLOutput out, CDOBranch branch) throws Exception
- {
- out.element(BRANCH);
- out.attribute(BRANCH_ID, branch.getID());
- out.attribute(BRANCH_NAME, branch.getName());
- out.attribute(BRANCH_TIME, branch.getBase().getTimeStamp());
- if (!branch.isMainBranch())
- {
- out.attribute(BRANCH_PARENT, branch.getBase().getBranch().getID());
- }
-
- out.push();
- super.exportBranch(out, branch);
- out.pop();
-
- }
-
- @Override
- protected void exportRevision(XMLOutput out, CDORevision revision) throws Exception
- {
- InternalCDORevision rev = (InternalCDORevision)revision;
-
- out.element(REVISION);
- out.attribute(REVISION_ID, str(rev.getID()));
- out.attribute(REVISION_CLASS, new CDOClassifierRef(rev.getEClass()).getURI());
- out.attribute(REVISION_VERSION, rev.getVersion());
- out.attribute(REVISION_TIME, rev.getTimeStamp());
-
- long revised = rev.getRevised();
- if (revised != CDOBranchPoint.UNSPECIFIED_DATE)
- {
- out.attribute(REVISION_REVISED, revised);
- }
-
- CDOID resourceID = rev.getResourceID();
- if (!CDOIDUtil.isNull(resourceID))
- {
- out.attribute(REVISION_RESOURCE, str(resourceID));
- }
-
- CDOID containerID = (CDOID)rev.getContainerID();
- if (!CDOIDUtil.isNull(containerID))
- {
- out.attribute(REVISION_CONTAINER, str(containerID));
- }
-
- int containingFeatureID = rev.getContainingFeatureID();
- if (containingFeatureID != 0)
- {
- out.attribute(REVISION_FEATURE, containingFeatureID);
- }
-
- out.push();
- CDOClassInfo classInfo = rev.getClassInfo();
- for (EStructuralFeature feature : classInfo.getAllPersistentFeatures())
- {
- if (feature.isMany())
- {
- @SuppressWarnings("unchecked")
- List<Object> list = (List<Object>)rev.getValue(feature);
- if (list != null)
- {
- for (Object value : list)
- {
- exportFeature(out, feature, value);
- }
- }
- }
- else
- {
- Object value = rev.getValue(feature);
- if (value != null)
- {
- exportFeature(out, feature, value);
- }
- }
- }
-
- out.pop();
- }
-
- protected void exportFeature(XMLOutput out, EStructuralFeature feature, Object value) throws Exception
- {
- out.element(FEATURE);
- out.attribute(FEATURE_NAME, feature.getName());
- exportFeature(out, feature, FEATURE_TYPE, value);
- }
-
- protected void exportFeature(XMLOutput out, EStructuralFeature feature, String featureType, Object value)
- throws SAXException
- {
- if (value instanceof CDOID)
- {
- out.attribute(featureType, Object.class.getSimpleName());
- out.attribute(FEATURE_VALUE, str((CDOID)value));
- }
- else if (value instanceof CDOBlob)
- {
- CDOBlob blob = (CDOBlob)value;
- out.attribute(featureType, TYPE_BLOB);
- out.attribute(FEATURE_ID, HexUtil.bytesToHex(blob.getID()));
- out.attribute(FEATURE_SIZE, blob.getSize());
- }
- else if (value instanceof CDOClob)
- {
- CDOClob clob = (CDOClob)value;
- out.attribute(featureType, TYPE_CLOB);
- out.attribute(FEATURE_ID, HexUtil.bytesToHex(clob.getID()));
- out.attribute(FEATURE_SIZE, clob.getSize());
- }
- else if (value instanceof Date)
- {
- Date date = (Date)value;
- out.attribute(featureType, Date.class.getSimpleName());
- out.attribute(FEATURE_VALUE, date.getTime());
- }
- else if (FeatureMapUtil.isFeatureMap(feature))
- {
- FeatureMap.Entry entry = (FeatureMap.Entry)value;
- EStructuralFeature innerFeature = entry.getEStructuralFeature();
- Object innerValue = entry.getValue();
-
- out.attribute(featureType, TYPE_FEATURE_MAP);
- out.attribute(FEATURE_INNER_FEATURE, innerFeature.getName());
- exportFeature(out, innerFeature, FEATURE_INNER_TYPE, innerValue);
- }
- else
- {
- if (!(value instanceof String))
- {
- out.attribute(featureType, type(value));
- }
-
- out.attributeOrNull(FEATURE_VALUE, value);
- }
- }
-
- @Override
- protected void exportLobs(XMLOutput out) throws Exception
- {
- out.element(LOBS);
-
- out.push();
- super.exportLobs(out);
- out.pop();
- }
-
- @Override
- protected OutputStream startBlob(XMLOutput out, byte[] id, long size) throws Exception
- {
- out.element(BLOB);
- out.attribute(LOB_ID, HexUtil.bytesToHex(id));
- out.attribute(LOB_SIZE, size);
- return out.bytes();
- }
-
- @Override
- protected Writer startClob(XMLOutput out, byte[] id, long size) throws Exception
- {
- out.element(CLOB);
- out.attribute(LOB_ID, HexUtil.bytesToHex(id));
- out.attribute(LOB_SIZE, size);
- return out.characters();
- }
-
- @Override
- protected void exportCommits(XMLOutput out) throws Exception
- {
- out.element(COMMITS);
-
- out.push();
- super.exportCommits(out);
- out.pop();
- }
-
- @Override
- protected void exportCommit(XMLOutput out, CDOCommitInfo commitInfo) throws Exception
- {
- out.element(COMMIT);
- out.attribute(COMMIT_TIME, commitInfo.getTimeStamp());
- long previous = commitInfo.getPreviousTimeStamp();
- if (previous != CDOBranchPoint.UNSPECIFIED_DATE)
- {
- out.attribute(COMMIT_PREVIOUS, previous);
- }
-
- int branch = commitInfo.getBranch().getID();
- if (branch != CDOBranch.MAIN_BRANCH_ID)
- {
- out.attribute(COMMIT_BRANCH, branch);
- }
-
- out.attribute(COMMIT_USER, commitInfo.getUserID());
- out.attribute(COMMIT_COMMENT, commitInfo.getComment());
- }
-
- protected final String str(CDOID id)
- {
- StringBuilder builder = new StringBuilder();
- CDOIDUtil.write(builder, id);
- return builder.toString();
- }
-
- protected String type(Object value)
- {
- if (value instanceof Boolean)
- {
- return Boolean.class.getSimpleName();
- }
-
- if (value instanceof Character)
- {
- return Character.class.getSimpleName();
- }
-
- if (value instanceof Byte)
- {
- return Byte.class.getSimpleName();
- }
-
- if (value instanceof Short)
- {
- return Short.class.getSimpleName();
- }
-
- if (value instanceof Integer)
- {
- return Integer.class.getSimpleName();
- }
-
- if (value instanceof Long)
- {
- return Long.class.getSimpleName();
- }
-
- if (value instanceof Float)
- {
- return Float.class.getSimpleName();
- }
-
- if (value instanceof Double)
- {
- return Double.class.getSimpleName();
- }
-
- if (value instanceof String)
- {
- return String.class.getSimpleName();
- }
-
- throw new IllegalArgumentException("Invalid type: " + value.getClass().getName());
- }
- }
-}
+/* + * Copyright (c) 2004 - 2012 Eike Stepper (Berlin, Germany) and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Eike Stepper - initial API and implementation + */ +package org.eclipse.emf.cdo.server; + +import org.eclipse.emf.cdo.common.branch.CDOBranch; +import org.eclipse.emf.cdo.common.branch.CDOBranchHandler; +import org.eclipse.emf.cdo.common.branch.CDOBranchPoint; +import org.eclipse.emf.cdo.common.commit.CDOCommitInfo; +import org.eclipse.emf.cdo.common.commit.CDOCommitInfoHandler; +import org.eclipse.emf.cdo.common.id.CDOID; +import org.eclipse.emf.cdo.common.id.CDOIDUtil; +import org.eclipse.emf.cdo.common.lob.CDOBlob; +import org.eclipse.emf.cdo.common.lob.CDOClob; +import org.eclipse.emf.cdo.common.lob.CDOLobHandler; +import org.eclipse.emf.cdo.common.model.CDOClassInfo; +import org.eclipse.emf.cdo.common.model.CDOClassifierRef; +import org.eclipse.emf.cdo.common.model.CDOPackageUnit; +import org.eclipse.emf.cdo.common.model.EMFUtil; +import org.eclipse.emf.cdo.common.revision.CDORevision; +import org.eclipse.emf.cdo.common.revision.CDORevisionHandler; +import org.eclipse.emf.cdo.spi.common.branch.InternalCDOBranchManager; +import org.eclipse.emf.cdo.spi.common.commit.InternalCDOCommitInfoManager; +import org.eclipse.emf.cdo.spi.common.model.InternalCDOPackageInfo; +import org.eclipse.emf.cdo.spi.common.model.InternalCDOPackageRegistry; +import org.eclipse.emf.cdo.spi.common.model.InternalCDOPackageUnit; +import org.eclipse.emf.cdo.spi.common.revision.InternalCDORevision; +import org.eclipse.emf.cdo.spi.server.InternalRepository; +import org.eclipse.emf.cdo.spi.server.InternalSession; + +import org.eclipse.net4j.util.HexUtil; +import org.eclipse.net4j.util.WrappedException; +import org.eclipse.net4j.util.io.XMLOutput; +import org.eclipse.net4j.util.lifecycle.LifecycleUtil; + +import org.eclipse.emf.ecore.EPackage; +import org.eclipse.emf.ecore.EStructuralFeature; +import org.eclipse.emf.ecore.util.FeatureMap; +import org.eclipse.emf.ecore.util.FeatureMapUtil; + +import org.xml.sax.SAXException; + +import java.io.OutputStream; +import java.io.Writer; +import java.math.BigDecimal; +import java.math.BigInteger; +import java.util.Date; +import java.util.List; + +/** + * Exports the complete contents of a {@link IRepository repository} in a format suitable for {@link CDOServerImporter + * imports} into new repositories. + * <p> + * Subtypes specifiy the actual exchange format. + * + * @author Eike Stepper + * @since 4.0 + */ +public abstract class CDOServerExporter<OUT> +{ + private InternalRepository repository; + + public CDOServerExporter(IRepository repository) + { + this.repository = (InternalRepository)repository; + } + + public final IRepository getRepository() + { + return repository; + } + + public final void exportRepository(OutputStream out) throws Exception + { + boolean wasActive = LifecycleUtil.isActive(repository); + if (!wasActive) + { + LifecycleUtil.activate(repository); + } + + InternalSession session = repository.getSessionManager().openSession(null); + StoreThreadLocal.setSession(session); + + try + { + OUT output = createOutput(out); + exportAll(output); + } + finally + { + StoreThreadLocal.release(); + if (!wasActive) + { + LifecycleUtil.deactivate(repository); + } + + repository = null; + } + } + + protected abstract OUT createOutput(OutputStream out) throws Exception; + + protected void exportAll(final OUT out) throws Exception + { + try + { + exportPackages(out); + exportBranches(out); + exportLobs(out); + exportCommits(out); + } + catch (WrappedException ex) + { + throw WrappedException.unwrap(ex); + } + } + + protected void exportPackages(OUT out) throws Exception + { + InternalCDOPackageRegistry packageRegistry = repository.getPackageRegistry(false); + InternalCDOPackageUnit[] packageUnits = packageRegistry.getPackageUnits(false); + for (InternalCDOPackageUnit packageUnit : packageUnits) + { + String id = packageUnit.getID(); + CDOPackageUnit.Type type = packageUnit.getOriginalType(); + long time = packageUnit.getTimeStamp(); + + EPackage ePackage = packageUnit.getTopLevelPackageInfo().getEPackage(); + String data = new String(EMFUtil.getEPackageBytes(ePackage, false, packageRegistry)); + + startPackageUnit(out, id, type, time, data); + for (InternalCDOPackageInfo packageInfo : packageUnit.getPackageInfos()) + { + String packageURI = packageInfo.getPackageURI(); + exportPackageInfo(out, packageURI); + } + + endPackageUnit(out); + } + } + + protected abstract void startPackageUnit(OUT out, String id, CDOPackageUnit.Type type, long time, String data) + throws Exception; + + protected abstract void endPackageUnit(OUT out) throws Exception; + + protected abstract void exportPackageInfo(OUT out, String packageURI) throws Exception; + + protected void exportBranches(final OUT out) throws Exception + { + InternalCDOBranchManager branchManager = repository.getBranchManager(); + exportBranch(out, branchManager.getMainBranch()); + + if (repository.isSupportingBranches()) + { + branchManager.getBranches(0, 0, new CDOBranchHandler() + { + public void handleBranch(CDOBranch branch) + { + try + { + exportBranch(out, branch); + } + catch (Exception ex) + { + throw WrappedException.wrap(ex); + } + } + }); + } + } + + protected void exportBranch(OUT out, CDOBranch branch) throws Exception + { + exportRevisions(out, branch); + } + + protected void exportRevisions(final OUT out, CDOBranch branch) throws Exception + { + repository.handleRevisions(null, branch, true, CDOBranchPoint.INVALID_DATE, false, + new CDORevisionHandler.Filtered.Undetached(new CDORevisionHandler() + { + public boolean handleRevision(CDORevision revision) + { + try + { + exportRevision(out, revision); + return true; + } + catch (Exception ex) + { + throw WrappedException.wrap(ex); + } + } + })); + } + + protected abstract void exportRevision(OUT out, CDORevision revision) throws Exception; + + protected void exportLobs(final OUT out) throws Exception + { + repository.handleLobs(0, 0, new CDOLobHandler() + { + public OutputStream handleBlob(byte[] id, long size) + { + try + { + return startBlob(out, id, size); + } + catch (Exception ex) + { + throw WrappedException.wrap(ex); + } + } + + public Writer handleClob(byte[] id, long size) + { + try + { + return startClob(out, id, size); + } + catch (Exception ex) + { + throw WrappedException.wrap(ex); + } + } + }); + } + + protected abstract OutputStream startBlob(OUT out, byte[] id, long size) throws Exception; + + protected abstract Writer startClob(OUT out, byte[] id, long size) throws Exception; + + protected void exportCommits(final OUT out) throws Exception + { + InternalCDOCommitInfoManager commitInfoManager = repository.getCommitInfoManager(); + commitInfoManager.getCommitInfos(null, 0L, 0L, new CDOCommitInfoHandler() + { + public void handleCommitInfo(CDOCommitInfo commitInfo) + { + try + { + exportCommit(out, commitInfo); + } + catch (Exception ex) + { + throw WrappedException.wrap(ex); + } + } + }); + } + + protected abstract void exportCommit(OUT out, CDOCommitInfo commitInfo) throws Exception; + + /** + * XML constants being used by both {@link CDOServerExporter exporters} and {@link CDOServerImporter importers}. + * + * @author Eike Stepper + */ + public static interface XMLConstants + { + public static final String REPOSITORY = "repository"; + + public static final String REPOSITORY_NAME = "name"; + + public static final String REPOSITORY_UUID = "uuid"; + + public static final String REPOSITORY_ROOT = "root"; + + public static final String REPOSITORY_CREATED = "created"; + + public static final String REPOSITORY_COMMITTED = "committed"; + + public static final String MODELS = "models"; + + public static final String PACKAGE_UNIT = "packageUnit"; + + public static final String PACKAGE_UNIT_ID = "id"; + + public static final String PACKAGE_UNIT_TYPE = "type"; + + public static final String PACKAGE_UNIT_TIME = "time"; + + public static final String PACKAGE_UNIT_DATA = "data"; + + public static final String PACKAGE_INFO = "packageInfo"; + + public static final String PACKAGE_INFO_URI = "uri"; + + public static final String INSTANCES = "instances"; + + public static final String BRANCH = "branch"; + + public static final String BRANCH_ID = "id"; + + public static final String BRANCH_NAME = "name"; + + public static final String BRANCH_TIME = "time"; + + public static final String BRANCH_PARENT = "parent"; + + public static final String REVISION = "revision"; + + public static final String REVISION_ID = "id"; + + public static final String REVISION_CLASS = "class"; + + public static final String REVISION_VERSION = "version"; + + public static final String REVISION_TIME = "time"; + + public static final String REVISION_REVISED = "revised"; + + public static final String REVISION_RESOURCE = "resource"; + + public static final String REVISION_CONTAINER = "container"; + + public static final String REVISION_FEATURE = "feature"; + + public static final String FEATURE = "feature"; + + public static final String FEATURE_NAME = "name"; + + public static final String FEATURE_TYPE = "type"; + + public static final String FEATURE_INNER_FEATURE = "innerFeature"; + + public static final String FEATURE_INNER_TYPE = "innerType"; + + public static final String FEATURE_VALUE = "value"; + + public static final String FEATURE_ID = "id"; + + public static final String FEATURE_SIZE = "size"; + + public static final String TYPE_BLOB = "Blob"; + + public static final String TYPE_CLOB = "Clob"; + + public static final String TYPE_FEATURE_MAP = "FeatureMap"; + + public static final String LOBS = "lobs"; + + public static final String LOB_ID = "id"; + + public static final String LOB_SIZE = "size"; + + public static final String BLOB = "blob"; + + public static final String CLOB = "clob"; + + public static final String COMMITS = "commits"; + + public static final String COMMIT = "commit"; + + public static final String COMMIT_TIME = "time"; + + public static final String COMMIT_PREVIOUS = "previous"; + + public static final String COMMIT_BRANCH = "branch"; + + public static final String COMMIT_USER = "user"; + + public static final String COMMIT_COMMENT = "comment"; + } + + /** + * An {@link CDOServerExporter exporter} that creates XML output suitable to be interpreted by an + * {@link CDOServerImporter.XML XML importer}. + * + * @author Eike Stepper + */ + public static class XML extends CDOServerExporter<XMLOutput> implements XMLConstants + { + public XML(IRepository repository) + { + super(repository); + } + + @Override + protected final XMLOutput createOutput(OutputStream out) throws Exception + { + return new XMLOutput(out); + } + + @Override + protected void exportAll(XMLOutput out) throws Exception + { + out.element(REPOSITORY); + out.attribute(REPOSITORY_NAME, getRepository().getName()); + out.attribute(REPOSITORY_UUID, getRepository().getUUID()); + out.attribute(REPOSITORY_ROOT, str(getRepository().getRootResourceID())); + out.attribute(REPOSITORY_CREATED, getRepository().getStore().getCreationTime()); + out.attribute(REPOSITORY_COMMITTED, getRepository().getLastCommitTimeStamp()); + + out.push(); + super.exportAll(out); + out.done(); + } + + @Override + protected void exportPackages(XMLOutput out) throws Exception + { + out.element(MODELS); + + out.push(); + super.exportPackages(out); + out.pop(); + } + + @Override + protected void startPackageUnit(XMLOutput out, String id, CDOPackageUnit.Type type, long time, String data) + throws Exception + { + out.element(PACKAGE_UNIT); + out.attribute(PACKAGE_UNIT_ID, id); + out.attribute(PACKAGE_UNIT_TYPE, type); + out.attribute(PACKAGE_UNIT_TIME, time); + out.attribute(PACKAGE_UNIT_DATA, data); + out.push(); + } + + @Override + protected void endPackageUnit(XMLOutput out) throws Exception + { + out.pop(); + } + + @Override + protected void exportPackageInfo(XMLOutput out, String uri) throws Exception + { + out.element(PACKAGE_INFO); + out.attribute(PACKAGE_INFO_URI, uri); + } + + @Override + protected void exportBranches(XMLOutput out) throws Exception + { + out.element(INSTANCES); + + out.push(); + super.exportBranches(out); + out.pop(); + } + + @Override + protected void exportBranch(XMLOutput out, CDOBranch branch) throws Exception + { + out.element(BRANCH); + out.attribute(BRANCH_ID, branch.getID()); + out.attribute(BRANCH_NAME, branch.getName()); + out.attribute(BRANCH_TIME, branch.getBase().getTimeStamp()); + if (!branch.isMainBranch()) + { + out.attribute(BRANCH_PARENT, branch.getBase().getBranch().getID()); + } + + out.push(); + super.exportBranch(out, branch); + out.pop(); + + } + + @Override + protected void exportRevision(XMLOutput out, CDORevision revision) throws Exception + { + InternalCDORevision rev = (InternalCDORevision)revision; + + out.element(REVISION); + out.attribute(REVISION_ID, str(rev.getID())); + out.attribute(REVISION_CLASS, new CDOClassifierRef(rev.getEClass()).getURI()); + out.attribute(REVISION_VERSION, rev.getVersion()); + out.attribute(REVISION_TIME, rev.getTimeStamp()); + + long revised = rev.getRevised(); + if (revised != CDOBranchPoint.UNSPECIFIED_DATE) + { + out.attribute(REVISION_REVISED, revised); + } + + CDOID resourceID = rev.getResourceID(); + if (!CDOIDUtil.isNull(resourceID)) + { + out.attribute(REVISION_RESOURCE, str(resourceID)); + } + + CDOID containerID = (CDOID)rev.getContainerID(); + if (!CDOIDUtil.isNull(containerID)) + { + out.attribute(REVISION_CONTAINER, str(containerID)); + } + + int containingFeatureID = rev.getContainingFeatureID(); + if (containingFeatureID != 0) + { + out.attribute(REVISION_FEATURE, containingFeatureID); + } + + out.push(); + CDOClassInfo classInfo = rev.getClassInfo(); + for (EStructuralFeature feature : classInfo.getAllPersistentFeatures()) + { + if (feature.isMany()) + { + @SuppressWarnings("unchecked") + List<Object> list = (List<Object>)rev.getValue(feature); + if (list != null) + { + for (Object value : list) + { + exportFeature(out, feature, value); + } + } + } + else + { + Object value = rev.getValue(feature); + if (value != null) + { + exportFeature(out, feature, value); + } + } + } + + out.pop(); + } + + protected void exportFeature(XMLOutput out, EStructuralFeature feature, Object value) throws Exception + { + out.element(FEATURE); + out.attribute(FEATURE_NAME, feature.getName()); + exportFeature(out, feature, FEATURE_TYPE, value); + } + + protected void exportFeature(XMLOutput out, EStructuralFeature feature, String featureType, Object value) + throws SAXException + { + if (value instanceof CDOID) + { + out.attribute(featureType, Object.class.getSimpleName()); + out.attribute(FEATURE_VALUE, str((CDOID)value)); + } + else if (value instanceof CDOBlob) + { + CDOBlob blob = (CDOBlob)value; + out.attribute(featureType, TYPE_BLOB); + out.attribute(FEATURE_ID, HexUtil.bytesToHex(blob.getID())); + out.attribute(FEATURE_SIZE, blob.getSize()); + } + else if (value instanceof CDOClob) + { + CDOClob clob = (CDOClob)value; + out.attribute(featureType, TYPE_CLOB); + out.attribute(FEATURE_ID, HexUtil.bytesToHex(clob.getID())); + out.attribute(FEATURE_SIZE, clob.getSize()); + } + else if (value instanceof Date) + { + Date date = (Date)value; + out.attribute(featureType, Date.class.getSimpleName()); + out.attribute(FEATURE_VALUE, date.getTime()); + } + else if (FeatureMapUtil.isFeatureMap(feature)) + { + FeatureMap.Entry entry = (FeatureMap.Entry)value; + EStructuralFeature innerFeature = entry.getEStructuralFeature(); + Object innerValue = entry.getValue(); + + out.attribute(featureType, TYPE_FEATURE_MAP); + out.attribute(FEATURE_INNER_FEATURE, innerFeature.getName()); + exportFeature(out, innerFeature, FEATURE_INNER_TYPE, innerValue); + } + else + { + if (!(value instanceof String)) + { + out.attribute(featureType, type(value)); + } + + out.attributeOrNull(FEATURE_VALUE, value); + } + } + + @Override + protected void exportLobs(XMLOutput out) throws Exception + { + out.element(LOBS); + + out.push(); + super.exportLobs(out); + out.pop(); + } + + @Override + protected OutputStream startBlob(XMLOutput out, byte[] id, long size) throws Exception + { + out.element(BLOB); + out.attribute(LOB_ID, HexUtil.bytesToHex(id)); + out.attribute(LOB_SIZE, size); + return out.bytes(); + } + + @Override + protected Writer startClob(XMLOutput out, byte[] id, long size) throws Exception + { + out.element(CLOB); + out.attribute(LOB_ID, HexUtil.bytesToHex(id)); + out.attribute(LOB_SIZE, size); + return out.characters(); + } + + @Override + protected void exportCommits(XMLOutput out) throws Exception + { + out.element(COMMITS); + + out.push(); + super.exportCommits(out); + out.pop(); + } + + @Override + protected void exportCommit(XMLOutput out, CDOCommitInfo commitInfo) throws Exception + { + out.element(COMMIT); + out.attribute(COMMIT_TIME, commitInfo.getTimeStamp()); + long previous = commitInfo.getPreviousTimeStamp(); + if (previous != CDOBranchPoint.UNSPECIFIED_DATE) + { + out.attribute(COMMIT_PREVIOUS, previous); + } + + int branch = commitInfo.getBranch().getID(); + if (branch != CDOBranch.MAIN_BRANCH_ID) + { + out.attribute(COMMIT_BRANCH, branch); + } + + out.attribute(COMMIT_USER, commitInfo.getUserID()); + out.attribute(COMMIT_COMMENT, commitInfo.getComment()); + } + + protected final String str(CDOID id) + { + StringBuilder builder = new StringBuilder(); + CDOIDUtil.write(builder, id); + return builder.toString(); + } + + protected String type(Object value) + { + if (value instanceof Boolean) + { + return Boolean.class.getSimpleName(); + } + + if (value instanceof Character) + { + return Character.class.getSimpleName(); + } + + if (value instanceof Byte) + { + return Byte.class.getSimpleName(); + } + + if (value instanceof Short) + { + return Short.class.getSimpleName(); + } + + if (value instanceof Integer) + { + return Integer.class.getSimpleName(); + } + + if (value instanceof Long) + { + return Long.class.getSimpleName(); + } + + if (value instanceof Float) + { + return Float.class.getSimpleName(); + } + + if (value instanceof Double) + { + return Double.class.getSimpleName(); + } + + if (value instanceof String) + { + return String.class.getSimpleName(); + } + + if (value instanceof BigDecimal) + { + return BigDecimal.class.getSimpleName(); + } + + if (value instanceof BigInteger) + { + return BigInteger.class.getSimpleName(); + } + + throw new IllegalArgumentException("Invalid type: " + value.getClass().getName()); + } + } +} diff --git a/plugins/org.eclipse.emf.cdo.server/src/org/eclipse/emf/cdo/server/CDOServerImporter.java b/plugins/org.eclipse.emf.cdo.server/src/org/eclipse/emf/cdo/server/CDOServerImporter.java index 2c4b0c8900..08870ef61b 100644 --- a/plugins/org.eclipse.emf.cdo.server/src/org/eclipse/emf/cdo/server/CDOServerImporter.java +++ b/plugins/org.eclipse.emf.cdo.server/src/org/eclipse/emf/cdo/server/CDOServerImporter.java @@ -1,673 +1,685 @@ -/*
- * Copyright (c) 2004 - 2012 Eike Stepper (Berlin, Germany) and others.
- * All rights reserved. This program and the accompanying materials
- * are made available under the terms of the Eclipse Public License v1.0
- * which accompanies this distribution, and is available at
- * http://www.eclipse.org/legal/epl-v10.html
- *
- * Contributors:
- * Eike Stepper - initial API and implementation
- */
-package org.eclipse.emf.cdo.server;
-
-import org.eclipse.emf.cdo.common.branch.CDOBranch;
-import org.eclipse.emf.cdo.common.branch.CDOBranchPoint;
-import org.eclipse.emf.cdo.common.id.CDOID;
-import org.eclipse.emf.cdo.common.id.CDOIDUtil;
-import org.eclipse.emf.cdo.common.lob.CDOLobHandler;
-import org.eclipse.emf.cdo.common.lob.CDOLobUtil;
-import org.eclipse.emf.cdo.common.model.CDOClassifierRef;
-import org.eclipse.emf.cdo.common.model.CDOModelUtil;
-import org.eclipse.emf.cdo.common.model.CDOPackageUnit;
-import org.eclipse.emf.cdo.common.model.CDOPackageUnit.Type;
-import org.eclipse.emf.cdo.common.model.EMFUtil;
-import org.eclipse.emf.cdo.common.model.EMFUtil.ExtResourceSet;
-import org.eclipse.emf.cdo.common.revision.CDOList;
-import org.eclipse.emf.cdo.common.revision.CDORevision;
-import org.eclipse.emf.cdo.common.revision.CDORevisionFactory;
-import org.eclipse.emf.cdo.common.revision.CDORevisionHandler;
-import org.eclipse.emf.cdo.common.revision.CDORevisionUtil;
-import org.eclipse.emf.cdo.spi.common.branch.InternalCDOBranch;
-import org.eclipse.emf.cdo.spi.common.branch.InternalCDOBranchManager;
-import org.eclipse.emf.cdo.spi.common.model.InternalCDOPackageInfo;
-import org.eclipse.emf.cdo.spi.common.model.InternalCDOPackageRegistry;
-import org.eclipse.emf.cdo.spi.common.model.InternalCDOPackageRegistry.PackageLoader;
-import org.eclipse.emf.cdo.spi.common.model.InternalCDOPackageUnit;
-import org.eclipse.emf.cdo.spi.common.revision.InternalCDORevision;
-import org.eclipse.emf.cdo.spi.server.InternalRepository;
-
-import org.eclipse.net4j.util.HexUtil;
-import org.eclipse.net4j.util.WrappedException;
-import org.eclipse.net4j.util.io.AsyncOutputStream;
-import org.eclipse.net4j.util.io.AsyncWriter;
-import org.eclipse.net4j.util.io.IOUtil;
-import org.eclipse.net4j.util.lifecycle.LifecycleUtil;
-import org.eclipse.net4j.util.om.monitor.Monitor;
-import org.eclipse.net4j.util.om.monitor.OMMonitor;
-
-import org.eclipse.emf.ecore.EClass;
-import org.eclipse.emf.ecore.EPackage;
-import org.eclipse.emf.ecore.EStructuralFeature;
-
-import org.xml.sax.Attributes;
-import org.xml.sax.SAXException;
-import org.xml.sax.helpers.DefaultHandler;
-
-import javax.xml.parsers.SAXParser;
-import javax.xml.parsers.SAXParserFactory;
-
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.OutputStream;
-import java.io.Reader;
-import java.io.Writer;
-import java.util.ArrayList;
-import java.util.Date;
-import java.util.HashMap;
-import java.util.LinkedList;
-import java.util.List;
-import java.util.Map;
-
-/**
- * Imports the complete contents of a {@link IRepository repository} from the output created by a
- * {@link CDOServerExporter exporter} into a new repository.
- * <p>
- * Subtypes specifiy the actual exchange format.
- *
- * @author Eike Stepper
- * @since 4.0
- * @apiviz.has {@link CDOServerImporter.Handler}
- */
-public abstract class CDOServerImporter
-{
- private InternalRepository repository;
-
- public CDOServerImporter(IRepository repository)
- {
- this.repository = (InternalRepository)repository;
- init();
- }
-
- private void init()
- {
- LifecycleUtil.checkInactive(repository);
- repository.setSkipInitialization(true);
- repository.getStore().setDropAllDataOnActivate(true);
- LifecycleUtil.activate(repository);
- }
-
- protected final InternalRepository getRepository()
- {
- return repository;
- }
-
- public void importRepository(InputStream in) throws Exception
- {
- try
- {
- FlushHandler handler = new FlushHandler();
- importAll(in, handler);
- handler.flush();
- }
- finally
- {
- StoreThreadLocal.release();
- repository = null;
- }
- }
-
- protected abstract void importAll(InputStream in, Handler handler) throws Exception;
-
- /**
- * Persists the data that has been read by a {@link CDOServerImporter importer} into a new {@link IRepository
- * repository}.
- *
- * @author Eike Stepper
- */
- public static interface Handler extends CDORevisionHandler, CDOLobHandler
- {
- public void handleRepository(String name, String uuid, CDOID root, long created, long committed);
-
- public InternalCDOPackageUnit handlePackageUnit(String id, Type type, long time, String data);
-
- public InternalCDOPackageInfo handlePackageInfo(String packageURI);
-
- public InternalCDOPackageRegistry handleModels();
-
- public InternalCDOBranch handleBranch(int id, String name, long time, int parentID);
-
- public void handleCommitInfo(long time, long previous, int branch, String user, String comment);
-
- public void flush();
- }
-
- /**
- * @author Eike Stepper
- */
- private final class FlushHandler implements Handler
- {
- private OMMonitor monitor = new Monitor();
-
- private IStoreAccessor.Raw accessor;
-
- private Map<String, String> models = new HashMap<String, String>();
-
- private LinkedList<InternalCDOPackageUnit> packageUnits = new LinkedList<InternalCDOPackageUnit>();
-
- private List<InternalCDOPackageInfo> packageInfos;
-
- private InternalCDOPackageRegistry packageRegistry = getRepository().getPackageRegistry(false);
-
- public FlushHandler()
- {
- }
-
- public void handleRepository(String name, String uuid, CDOID root, long created, long committed)
- {
- repository.getStore().setCreationTime(created);
- repository.getStore().setLastCommitTime(committed);
-
- InternalCDOBranchManager branchManager = repository.getBranchManager();
- repository.initMainBranch(branchManager, created);
- LifecycleUtil.activate(branchManager);
-
- repository.initSystemPackages();
- repository.setRootResourceID(root);
-
- // InternalSession session = repository.getSessionManager().openSession(null);
- // StoreThreadLocal.setSession(session);
-
- accessor = (IStoreAccessor.Raw)repository.getStore().getWriter(null);
- StoreThreadLocal.setAccessor(accessor);
- }
-
- public InternalCDOPackageUnit handlePackageUnit(String id, Type type, long time, String data)
- {
- collectPackageInfos();
-
- InternalCDOPackageUnit packageUnit = packageRegistry.createPackageUnit();
- packageUnit.setOriginalType(type);
- packageUnit.setTimeStamp(time);
-
- models.put(id, data);
- packageUnits.add(packageUnit);
- packageInfos = new ArrayList<InternalCDOPackageInfo>();
- return packageUnit;
- }
-
- public InternalCDOPackageInfo handlePackageInfo(String packageURI)
- {
- InternalCDOPackageInfo packageInfo = (InternalCDOPackageInfo)CDOModelUtil.createPackageInfo();
- packageInfo.setPackageURI(packageURI);
- packageInfos.add(packageInfo);
- return packageInfo;
- }
-
- public InternalCDOPackageRegistry handleModels()
- {
- collectPackageInfos();
- InternalCDOPackageUnit[] array = packageUnits.toArray(new InternalCDOPackageUnit[packageUnits.size()]);
- packageUnits = null;
-
- final ExtResourceSet resourceSet = EMFUtil.createExtResourceSet(packageRegistry, false, false);
-
- PackageLoader loader = new PackageLoader()
- {
- public EPackage[] loadPackages(CDOPackageUnit packageUnit)
- {
- String id = packageUnit.getID();
- String data = models.get(id);
-
- EPackage ePackage = EMFUtil.createEPackage(id, data.getBytes(), false, resourceSet, true);
- return EMFUtil.getAllPackages(ePackage);
- }
- };
-
- packageRegistry.putPackageUnits(array, CDOPackageUnit.State.PROXY);
- for (InternalCDOPackageUnit packageUnit : array)
- {
- packageUnit.load(loader, false);
- }
-
- // Before we resolve, we configure the resourceSet to start delegating, which means
- // it will consult the packageRegistry for packages it didn't just load -- such as the Ecore
- // package
- resourceSet.setDelegating(true);
- EMFUtil.safeResolveAll(resourceSet);
-
- accessor.rawStore(array, monitor);
-
- return packageRegistry;
- }
-
- public InternalCDOBranch handleBranch(int id, String name, long time, int parentID)
- {
- InternalCDOBranchManager branchManager = repository.getBranchManager();
- if (id == CDOBranch.MAIN_BRANCH_ID)
- {
- return branchManager.getMainBranch();
- }
-
- InternalCDOBranch parent = branchManager.getBranch(parentID);
- return branchManager.createBranch(id, name, parent, time);
- }
-
- public boolean handleRevision(CDORevision revision)
- {
- accessor.rawStore((InternalCDORevision)revision, monitor);
- return true;
- }
-
- public OutputStream handleBlob(final byte[] id, final long size) throws IOException
- {
- return new AsyncOutputStream()
- {
- @Override
- protected void asyncWrite(InputStream in) throws IOException
- {
- accessor.rawStore(id, size, in);
- }
- };
- }
-
- public Writer handleClob(final byte[] id, final long size) throws IOException
- {
- return new AsyncWriter()
- {
- @Override
- protected void asyncWrite(Reader in) throws IOException
- {
- accessor.rawStore(id, size, in);
- }
- };
- }
-
- public void handleCommitInfo(long time, long previous, int branchID, String user, String comment)
- {
- CDOBranch branch = repository.getBranchManager().getBranch(branchID);
- accessor.rawStore(branch, time, previous, user, comment, monitor);
- }
-
- public void flush()
- {
- accessor.rawCommit(1.0, monitor);
- }
-
- private void collectPackageInfos()
- {
- if (packageInfos != null)
- {
- InternalCDOPackageUnit packageUnit = packageUnits.getLast();
- packageUnit.setPackageInfos(packageInfos.toArray(new InternalCDOPackageInfo[packageInfos.size()]));
- packageInfos = null;
- }
- }
- }
-
- /**
- * An {@link CDOServerImporter importer} that reads and interprets XML output created by an
- * {@link CDOServerExporter.XML XML exporter}.
- *
- * @author Eike Stepper
- */
- public static class XML extends CDOServerImporter implements CDOServerExporter.XMLConstants
- {
- public XML(IRepository repository)
- {
- super(repository);
- }
-
- @Override
- protected void importAll(InputStream in, final Handler handler) throws Exception
- {
- DefaultHandler xmlHandler = new XMLHandler(handler);
-
- SAXParserFactory factory = SAXParserFactory.newInstance();
- SAXParser saxParser = factory.newSAXParser();
- saxParser.parse(in, xmlHandler);
- }
-
- /**
- * @author Eike Stepper
- */
- private static final class XMLHandler extends DefaultHandler
- {
- private Handler handler;
-
- private InternalCDOPackageRegistry packageRegistry;
-
- private InternalCDOBranch branch;
-
- private InternalCDORevision revision;
-
- private Character blobChar;
-
- private OutputStream blob;
-
- private Writer clob;
-
- private XMLHandler(Handler handler)
- {
- this.handler = handler;
- }
-
- @Override
- public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException
- {
- if (REPOSITORY.equals(qName))
- {
- String name = attributes.getValue(REPOSITORY_NAME);
- String uuid = attributes.getValue(REPOSITORY_UUID);
- CDOID root = id(attributes.getValue(REPOSITORY_ROOT));
- long created = Long.parseLong(attributes.getValue(REPOSITORY_CREATED));
- long committed = Long.parseLong(attributes.getValue(REPOSITORY_COMMITTED));
- handler.handleRepository(name, uuid, root, created, committed);
- }
- else if (PACKAGE_UNIT.equals(qName))
- {
- String id = attributes.getValue(PACKAGE_UNIT_ID);
- Type type = CDOPackageUnit.Type.valueOf(attributes.getValue(PACKAGE_UNIT_TYPE));
- long time = Long.parseLong(attributes.getValue(PACKAGE_UNIT_TIME));
- String data = attributes.getValue(PACKAGE_UNIT_DATA);
- handler.handlePackageUnit(id, type, time, data);
- }
- else if (PACKAGE_INFO.equals(qName))
- {
- String packageURI = attributes.getValue(PACKAGE_INFO_URI);
- handler.handlePackageInfo(packageURI);
- }
- else if (BRANCH.equals(qName))
- {
- int id = Integer.parseInt(attributes.getValue(BRANCH_ID));
- String name = attributes.getValue(BRANCH_NAME);
- long time = Long.parseLong(attributes.getValue(BRANCH_TIME));
- String parent = attributes.getValue(BRANCH_PARENT);
- int parentID = parent == null ? 0 : Integer.parseInt(parent);
- branch = handler.handleBranch(id, name, time, parentID);
- }
- else if (REVISION.equals(qName))
- {
- CDOClassifierRef classifierRef = new CDOClassifierRef(attributes.getValue(REVISION_CLASS));
- EClass eClass = (EClass)classifierRef.resolve(packageRegistry);
- revision = (InternalCDORevision)CDORevisionFactory.DEFAULT.createRevision(eClass);
- revision.setID(id(attributes.getValue(REVISION_ID)));
- revision.setBranchPoint(branch.getPoint(Long.parseLong(attributes.getValue(REVISION_TIME))));
- revision.setVersion(Integer.parseInt(attributes.getValue(REVISION_VERSION)));
- String revised = attributes.getValue(REVISION_REVISED);
- if (revised != null)
- {
- revision.setRevised(Long.parseLong(revised));
- }
-
- String resourceID = attributes.getValue(REVISION_RESOURCE);
- if (resourceID != null)
- {
- revision.setResourceID(id(resourceID));
- }
-
- String containerID = attributes.getValue(REVISION_CONTAINER);
- if (containerID != null)
- {
- revision.setContainerID(id(containerID));
- }
-
- String featureID = attributes.getValue(REVISION_FEATURE);
- if (featureID != null)
- {
- revision.setContainingFeatureID(Integer.parseInt(featureID));
- }
- }
- else if (FEATURE.equals(qName))
- {
- String name = attributes.getValue(FEATURE_NAME);
- Object value = value(attributes);
-
- EClass eClass = revision.getEClass();
- EStructuralFeature feature = eClass.getEStructuralFeature(name);
- if (feature == null)
- {
- throw new IllegalStateException("Feature " + name + " not found in class " + eClass.getName());
- }
-
- if (feature.isMany())
- {
- CDOList list = revision.getList(feature);
- list.add(value);
- }
- else
- {
- if (value != null)
- {
- revision.setValue(feature, value);
- }
- }
- }
- else if (BLOB.equals(qName))
- {
- try
- {
- byte[] id = HexUtil.hexToBytes(attributes.getValue(LOB_ID));
- long size = Long.parseLong(attributes.getValue(LOB_SIZE));
- blob = handler.handleBlob(id, size);
- }
- catch (IOException ex)
- {
- throw WrappedException.wrap(ex);
- }
- }
- else if (CLOB.equals(qName))
- {
- try
- {
- byte[] id = HexUtil.hexToBytes(attributes.getValue(LOB_ID));
- long size = Long.parseLong(attributes.getValue(LOB_SIZE));
- clob = handler.handleClob(id, size);
- }
- catch (IOException ex)
- {
- throw WrappedException.wrap(ex);
- }
- }
- else if (COMMIT.equals(qName))
- {
- long time = Long.parseLong(attributes.getValue(COMMIT_TIME));
-
- String value = attributes.getValue(COMMIT_PREVIOUS);
- long previous = value == null ? CDOBranchPoint.UNSPECIFIED_DATE : Long.parseLong(value);
-
- value = attributes.getValue(COMMIT_BRANCH);
- int branch = value == null ? CDOBranch.MAIN_BRANCH_ID : Integer.parseInt(value);
-
- String user = attributes.getValue(COMMIT_USER);
- String comment = attributes.getValue(COMMIT_COMMENT);
-
- handler.handleCommitInfo(time, previous, branch, user, comment);
- }
- }
-
- @Override
- public void characters(char[] ch, int start, int length) throws SAXException
- {
- if (blob != null)
- {
- try
- {
- if (blobChar != null)
- {
- char[] firstChars = { blobChar, ch[start] };
- blobChar = null;
-
- byte[] firstByte = HexUtil.hexToBytes(new String(firstChars));
- blob.write(firstByte, 0, 1);
-
- ++start;
- --length;
- }
-
- if ((length & 1) == 1) // odd length?
- {
- --length;
- blobChar = ch[length];
- }
-
- if (start != 0 || length != ch.length)
- {
- char[] tmp = new char[length];
- System.arraycopy(ch, start, tmp, 0, length);
- ch = tmp;
- }
-
- byte[] buf = HexUtil.hexToBytes(new String(ch));
- blob.write(buf);
- }
- catch (IOException ex)
- {
- throw WrappedException.wrap(ex);
- }
- }
- else if (clob != null)
- {
- try
- {
- clob.write(ch, start, length);
- }
- catch (IOException ex)
- {
- throw WrappedException.wrap(ex);
- }
- }
- }
-
- @Override
- public void endElement(String uri, String localName, String qName) throws SAXException
- {
- if (MODELS.equals(qName))
- {
- packageRegistry = handler.handleModels();
- }
- else if (BRANCH.equals(qName))
- {
- branch = null;
- }
- else if (REVISION.equals(qName))
- {
- handler.handleRevision(revision);
- revision = null;
- }
- else if (BLOB.equals(qName))
- {
- IOUtil.close(blob);
- blob = null;
- }
- else if (CLOB.equals(qName))
- {
- IOUtil.close(clob);
- clob = null;
- }
- }
-
- protected final CDOID id(String str)
- {
- return CDOIDUtil.read(str);
- }
-
- protected Object value(Attributes attributes)
- {
- String type = attributes.getValue(FEATURE_TYPE);
-
- if (TYPE_BLOB.equals(type))
- {
- byte[] id = HexUtil.hexToBytes(attributes.getValue(FEATURE_ID));
- long size = Long.parseLong(attributes.getValue(FEATURE_SIZE));
- return CDOLobUtil.createBlob(id, size);
- }
-
- if (TYPE_CLOB.equals(type))
- {
- byte[] id = HexUtil.hexToBytes(attributes.getValue(FEATURE_ID));
- long size = Long.parseLong(attributes.getValue(FEATURE_SIZE));
- return CDOLobUtil.createClob(id, size);
- }
-
- if (TYPE_FEATURE_MAP.equals(type))
- {
- String innerFeatureName = attributes.getValue(FEATURE_INNER_FEATURE);
- EStructuralFeature innerFeature = revision.getEClass().getEStructuralFeature(innerFeatureName);
-
- String innerType = attributes.getValue(FEATURE_INNER_TYPE);
- Object innerValue = value(attributes, innerType);
-
- return CDORevisionUtil.createFeatureMapEntry(innerFeature, innerValue);
- }
-
- return value(attributes, type);
- }
-
- protected Object value(Attributes attributes, String type)
- {
- String str = attributes.getValue(FEATURE_VALUE);
- if (str == null)
- {
- return null;
- }
-
- if (type == null || String.class.getSimpleName().equals(type))
- {
- return str;
- }
-
- if (Object.class.getSimpleName().equals(type))
- {
- return id(str);
- }
-
- if (Boolean.class.getSimpleName().equals(type))
- {
- return Boolean.valueOf(str);
- }
-
- if (Character.class.getSimpleName().equals(type))
- {
- return str.charAt(0);
- }
-
- if (Byte.class.getSimpleName().equals(type))
- {
- return Byte.valueOf(str);
- }
-
- if (Short.class.getSimpleName().equals(type))
- {
- return Short.valueOf(str);
- }
-
- if (Integer.class.getSimpleName().equals(type))
- {
- return Integer.valueOf(str);
- }
-
- if (Long.class.getSimpleName().equals(type))
- {
- return Long.valueOf(str);
- }
-
- if (Float.class.getSimpleName().equals(type))
- {
- return Float.valueOf(str);
- }
-
- if (Double.class.getSimpleName().equals(type))
- {
- return Double.valueOf(str);
- }
-
- if (Date.class.getSimpleName().equals(type))
- {
- return new Date(Long.valueOf(str));
- }
-
- throw new IllegalArgumentException("Invalid type: " + type);
- }
- }
- }
-}
+/* + * Copyright (c) 2004 - 2012 Eike Stepper (Berlin, Germany) and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Eike Stepper - initial API and implementation + */ +package org.eclipse.emf.cdo.server; + +import org.eclipse.emf.cdo.common.branch.CDOBranch; +import org.eclipse.emf.cdo.common.branch.CDOBranchPoint; +import org.eclipse.emf.cdo.common.id.CDOID; +import org.eclipse.emf.cdo.common.id.CDOIDUtil; +import org.eclipse.emf.cdo.common.lob.CDOLobHandler; +import org.eclipse.emf.cdo.common.lob.CDOLobUtil; +import org.eclipse.emf.cdo.common.model.CDOClassifierRef; +import org.eclipse.emf.cdo.common.model.CDOModelUtil; +import org.eclipse.emf.cdo.common.model.CDOPackageUnit; +import org.eclipse.emf.cdo.common.model.CDOPackageUnit.Type; +import org.eclipse.emf.cdo.common.model.EMFUtil; +import org.eclipse.emf.cdo.common.model.EMFUtil.ExtResourceSet; +import org.eclipse.emf.cdo.common.revision.CDOList; +import org.eclipse.emf.cdo.common.revision.CDORevision; +import org.eclipse.emf.cdo.common.revision.CDORevisionFactory; +import org.eclipse.emf.cdo.common.revision.CDORevisionHandler; +import org.eclipse.emf.cdo.common.revision.CDORevisionUtil; +import org.eclipse.emf.cdo.spi.common.branch.InternalCDOBranch; +import org.eclipse.emf.cdo.spi.common.branch.InternalCDOBranchManager; +import org.eclipse.emf.cdo.spi.common.model.InternalCDOPackageInfo; +import org.eclipse.emf.cdo.spi.common.model.InternalCDOPackageRegistry; +import org.eclipse.emf.cdo.spi.common.model.InternalCDOPackageRegistry.PackageLoader; +import org.eclipse.emf.cdo.spi.common.model.InternalCDOPackageUnit; +import org.eclipse.emf.cdo.spi.common.revision.InternalCDORevision; +import org.eclipse.emf.cdo.spi.server.InternalRepository; + +import org.eclipse.net4j.util.HexUtil; +import org.eclipse.net4j.util.WrappedException; +import org.eclipse.net4j.util.io.AsyncOutputStream; +import org.eclipse.net4j.util.io.AsyncWriter; +import org.eclipse.net4j.util.io.IOUtil; +import org.eclipse.net4j.util.lifecycle.LifecycleUtil; +import org.eclipse.net4j.util.om.monitor.Monitor; +import org.eclipse.net4j.util.om.monitor.OMMonitor; + +import org.eclipse.emf.ecore.EClass; +import org.eclipse.emf.ecore.EPackage; +import org.eclipse.emf.ecore.EStructuralFeature; + +import org.xml.sax.Attributes; +import org.xml.sax.SAXException; +import org.xml.sax.helpers.DefaultHandler; + +import javax.xml.parsers.SAXParser; +import javax.xml.parsers.SAXParserFactory; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.io.Reader; +import java.io.Writer; +import java.math.BigDecimal; +import java.math.BigInteger; +import java.util.ArrayList; +import java.util.Date; +import java.util.HashMap; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; + +/** + * Imports the complete contents of a {@link IRepository repository} from the output created by a + * {@link CDOServerExporter exporter} into a new repository. + * <p> + * Subtypes specifiy the actual exchange format. + * + * @author Eike Stepper + * @since 4.0 + * @apiviz.has {@link CDOServerImporter.Handler} + */ +public abstract class CDOServerImporter +{ + private InternalRepository repository; + + public CDOServerImporter(IRepository repository) + { + this.repository = (InternalRepository)repository; + init(); + } + + private void init() + { + LifecycleUtil.checkInactive(repository); + repository.setSkipInitialization(true); + repository.getStore().setDropAllDataOnActivate(true); + LifecycleUtil.activate(repository); + } + + protected final InternalRepository getRepository() + { + return repository; + } + + public void importRepository(InputStream in) throws Exception + { + try + { + FlushHandler handler = new FlushHandler(); + importAll(in, handler); + handler.flush(); + } + finally + { + StoreThreadLocal.release(); + repository = null; + } + } + + protected abstract void importAll(InputStream in, Handler handler) throws Exception; + + /** + * Persists the data that has been read by a {@link CDOServerImporter importer} into a new {@link IRepository + * repository}. + * + * @author Eike Stepper + */ + public static interface Handler extends CDORevisionHandler, CDOLobHandler + { + public void handleRepository(String name, String uuid, CDOID root, long created, long committed); + + public InternalCDOPackageUnit handlePackageUnit(String id, Type type, long time, String data); + + public InternalCDOPackageInfo handlePackageInfo(String packageURI); + + public InternalCDOPackageRegistry handleModels(); + + public InternalCDOBranch handleBranch(int id, String name, long time, int parentID); + + public void handleCommitInfo(long time, long previous, int branch, String user, String comment); + + public void flush(); + } + + /** + * @author Eike Stepper + */ + private final class FlushHandler implements Handler + { + private OMMonitor monitor = new Monitor(); + + private IStoreAccessor.Raw accessor; + + private Map<String, String> models = new HashMap<String, String>(); + + private LinkedList<InternalCDOPackageUnit> packageUnits = new LinkedList<InternalCDOPackageUnit>(); + + private List<InternalCDOPackageInfo> packageInfos; + + private InternalCDOPackageRegistry packageRegistry = getRepository().getPackageRegistry(false); + + public FlushHandler() + { + } + + public void handleRepository(String name, String uuid, CDOID root, long created, long committed) + { + repository.getStore().setCreationTime(created); + repository.getStore().setLastCommitTime(committed); + + InternalCDOBranchManager branchManager = repository.getBranchManager(); + repository.initMainBranch(branchManager, created); + LifecycleUtil.activate(branchManager); + + repository.initSystemPackages(); + repository.setRootResourceID(root); + + // InternalSession session = repository.getSessionManager().openSession(null); + // StoreThreadLocal.setSession(session); + + accessor = (IStoreAccessor.Raw)repository.getStore().getWriter(null); + StoreThreadLocal.setAccessor(accessor); + } + + public InternalCDOPackageUnit handlePackageUnit(String id, Type type, long time, String data) + { + collectPackageInfos(); + + InternalCDOPackageUnit packageUnit = packageRegistry.createPackageUnit(); + packageUnit.setOriginalType(type); + packageUnit.setTimeStamp(time); + + models.put(id, data); + packageUnits.add(packageUnit); + packageInfos = new ArrayList<InternalCDOPackageInfo>(); + return packageUnit; + } + + public InternalCDOPackageInfo handlePackageInfo(String packageURI) + { + InternalCDOPackageInfo packageInfo = (InternalCDOPackageInfo)CDOModelUtil.createPackageInfo(); + packageInfo.setPackageURI(packageURI); + packageInfos.add(packageInfo); + return packageInfo; + } + + public InternalCDOPackageRegistry handleModels() + { + collectPackageInfos(); + InternalCDOPackageUnit[] array = packageUnits.toArray(new InternalCDOPackageUnit[packageUnits.size()]); + packageUnits = null; + + final ExtResourceSet resourceSet = EMFUtil.createExtResourceSet(packageRegistry, false, false); + + PackageLoader loader = new PackageLoader() + { + public EPackage[] loadPackages(CDOPackageUnit packageUnit) + { + String id = packageUnit.getID(); + String data = models.get(id); + + EPackage ePackage = EMFUtil.createEPackage(id, data.getBytes(), false, resourceSet, true); + return EMFUtil.getAllPackages(ePackage); + } + }; + + packageRegistry.putPackageUnits(array, CDOPackageUnit.State.PROXY); + for (InternalCDOPackageUnit packageUnit : array) + { + packageUnit.load(loader, false); + } + + // Before we resolve, we configure the resourceSet to start delegating, which means + // it will consult the packageRegistry for packages it didn't just load -- such as the Ecore + // package + resourceSet.setDelegating(true); + EMFUtil.safeResolveAll(resourceSet); + + accessor.rawStore(array, monitor); + + return packageRegistry; + } + + public InternalCDOBranch handleBranch(int id, String name, long time, int parentID) + { + InternalCDOBranchManager branchManager = repository.getBranchManager(); + if (id == CDOBranch.MAIN_BRANCH_ID) + { + return branchManager.getMainBranch(); + } + + InternalCDOBranch parent = branchManager.getBranch(parentID); + return branchManager.createBranch(id, name, parent, time); + } + + public boolean handleRevision(CDORevision revision) + { + accessor.rawStore((InternalCDORevision)revision, monitor); + return true; + } + + public OutputStream handleBlob(final byte[] id, final long size) throws IOException + { + return new AsyncOutputStream() + { + @Override + protected void asyncWrite(InputStream in) throws IOException + { + accessor.rawStore(id, size, in); + } + }; + } + + public Writer handleClob(final byte[] id, final long size) throws IOException + { + return new AsyncWriter() + { + @Override + protected void asyncWrite(Reader in) throws IOException + { + accessor.rawStore(id, size, in); + } + }; + } + + public void handleCommitInfo(long time, long previous, int branchID, String user, String comment) + { + CDOBranch branch = repository.getBranchManager().getBranch(branchID); + accessor.rawStore(branch, time, previous, user, comment, monitor); + } + + public void flush() + { + accessor.rawCommit(1.0, monitor); + } + + private void collectPackageInfos() + { + if (packageInfos != null) + { + InternalCDOPackageUnit packageUnit = packageUnits.getLast(); + packageUnit.setPackageInfos(packageInfos.toArray(new InternalCDOPackageInfo[packageInfos.size()])); + packageInfos = null; + } + } + } + + /** + * An {@link CDOServerImporter importer} that reads and interprets XML output created by an + * {@link CDOServerExporter.XML XML exporter}. + * + * @author Eike Stepper + */ + public static class XML extends CDOServerImporter implements CDOServerExporter.XMLConstants + { + public XML(IRepository repository) + { + super(repository); + } + + @Override + protected void importAll(InputStream in, final Handler handler) throws Exception + { + DefaultHandler xmlHandler = new XMLHandler(handler); + + SAXParserFactory factory = SAXParserFactory.newInstance(); + SAXParser saxParser = factory.newSAXParser(); + saxParser.parse(in, xmlHandler); + } + + /** + * @author Eike Stepper + */ + private static final class XMLHandler extends DefaultHandler + { + private Handler handler; + + private InternalCDOPackageRegistry packageRegistry; + + private InternalCDOBranch branch; + + private InternalCDORevision revision; + + private Character blobChar; + + private OutputStream blob; + + private Writer clob; + + private XMLHandler(Handler handler) + { + this.handler = handler; + } + + @Override + public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException + { + if (REPOSITORY.equals(qName)) + { + String name = attributes.getValue(REPOSITORY_NAME); + String uuid = attributes.getValue(REPOSITORY_UUID); + CDOID root = id(attributes.getValue(REPOSITORY_ROOT)); + long created = Long.parseLong(attributes.getValue(REPOSITORY_CREATED)); + long committed = Long.parseLong(attributes.getValue(REPOSITORY_COMMITTED)); + handler.handleRepository(name, uuid, root, created, committed); + } + else if (PACKAGE_UNIT.equals(qName)) + { + String id = attributes.getValue(PACKAGE_UNIT_ID); + Type type = CDOPackageUnit.Type.valueOf(attributes.getValue(PACKAGE_UNIT_TYPE)); + long time = Long.parseLong(attributes.getValue(PACKAGE_UNIT_TIME)); + String data = attributes.getValue(PACKAGE_UNIT_DATA); + handler.handlePackageUnit(id, type, time, data); + } + else if (PACKAGE_INFO.equals(qName)) + { + String packageURI = attributes.getValue(PACKAGE_INFO_URI); + handler.handlePackageInfo(packageURI); + } + else if (BRANCH.equals(qName)) + { + int id = Integer.parseInt(attributes.getValue(BRANCH_ID)); + String name = attributes.getValue(BRANCH_NAME); + long time = Long.parseLong(attributes.getValue(BRANCH_TIME)); + String parent = attributes.getValue(BRANCH_PARENT); + int parentID = parent == null ? 0 : Integer.parseInt(parent); + branch = handler.handleBranch(id, name, time, parentID); + } + else if (REVISION.equals(qName)) + { + CDOClassifierRef classifierRef = new CDOClassifierRef(attributes.getValue(REVISION_CLASS)); + EClass eClass = (EClass)classifierRef.resolve(packageRegistry); + revision = (InternalCDORevision)CDORevisionFactory.DEFAULT.createRevision(eClass); + revision.setID(id(attributes.getValue(REVISION_ID))); + revision.setBranchPoint(branch.getPoint(Long.parseLong(attributes.getValue(REVISION_TIME)))); + revision.setVersion(Integer.parseInt(attributes.getValue(REVISION_VERSION))); + String revised = attributes.getValue(REVISION_REVISED); + if (revised != null) + { + revision.setRevised(Long.parseLong(revised)); + } + + String resourceID = attributes.getValue(REVISION_RESOURCE); + if (resourceID != null) + { + revision.setResourceID(id(resourceID)); + } + + String containerID = attributes.getValue(REVISION_CONTAINER); + if (containerID != null) + { + revision.setContainerID(id(containerID)); + } + + String featureID = attributes.getValue(REVISION_FEATURE); + if (featureID != null) + { + revision.setContainingFeatureID(Integer.parseInt(featureID)); + } + } + else if (FEATURE.equals(qName)) + { + String name = attributes.getValue(FEATURE_NAME); + Object value = value(attributes); + + EClass eClass = revision.getEClass(); + EStructuralFeature feature = eClass.getEStructuralFeature(name); + if (feature == null) + { + throw new IllegalStateException("Feature " + name + " not found in class " + eClass.getName()); + } + + if (feature.isMany()) + { + CDOList list = revision.getList(feature); + list.add(value); + } + else + { + if (value != null) + { + revision.setValue(feature, value); + } + } + } + else if (BLOB.equals(qName)) + { + try + { + byte[] id = HexUtil.hexToBytes(attributes.getValue(LOB_ID)); + long size = Long.parseLong(attributes.getValue(LOB_SIZE)); + blob = handler.handleBlob(id, size); + } + catch (IOException ex) + { + throw WrappedException.wrap(ex); + } + } + else if (CLOB.equals(qName)) + { + try + { + byte[] id = HexUtil.hexToBytes(attributes.getValue(LOB_ID)); + long size = Long.parseLong(attributes.getValue(LOB_SIZE)); + clob = handler.handleClob(id, size); + } + catch (IOException ex) + { + throw WrappedException.wrap(ex); + } + } + else if (COMMIT.equals(qName)) + { + long time = Long.parseLong(attributes.getValue(COMMIT_TIME)); + + String value = attributes.getValue(COMMIT_PREVIOUS); + long previous = value == null ? CDOBranchPoint.UNSPECIFIED_DATE : Long.parseLong(value); + + value = attributes.getValue(COMMIT_BRANCH); + int branch = value == null ? CDOBranch.MAIN_BRANCH_ID : Integer.parseInt(value); + + String user = attributes.getValue(COMMIT_USER); + String comment = attributes.getValue(COMMIT_COMMENT); + + handler.handleCommitInfo(time, previous, branch, user, comment); + } + } + + @Override + public void characters(char[] ch, int start, int length) throws SAXException + { + if (blob != null) + { + try + { + if (blobChar != null) + { + char[] firstChars = { blobChar, ch[start] }; + blobChar = null; + + byte[] firstByte = HexUtil.hexToBytes(new String(firstChars)); + blob.write(firstByte, 0, 1); + + ++start; + --length; + } + + if ((length & 1) == 1) // odd length? + { + --length; + blobChar = ch[length]; + } + + if (start != 0 || length != ch.length) + { + char[] tmp = new char[length]; + System.arraycopy(ch, start, tmp, 0, length); + ch = tmp; + } + + byte[] buf = HexUtil.hexToBytes(new String(ch)); + blob.write(buf); + } + catch (IOException ex) + { + throw WrappedException.wrap(ex); + } + } + else if (clob != null) + { + try + { + clob.write(ch, start, length); + } + catch (IOException ex) + { + throw WrappedException.wrap(ex); + } + } + } + + @Override + public void endElement(String uri, String localName, String qName) throws SAXException + { + if (MODELS.equals(qName)) + { + packageRegistry = handler.handleModels(); + } + else if (BRANCH.equals(qName)) + { + branch = null; + } + else if (REVISION.equals(qName)) + { + handler.handleRevision(revision); + revision = null; + } + else if (BLOB.equals(qName)) + { + IOUtil.close(blob); + blob = null; + } + else if (CLOB.equals(qName)) + { + IOUtil.close(clob); + clob = null; + } + } + + protected final CDOID id(String str) + { + return CDOIDUtil.read(str); + } + + protected Object value(Attributes attributes) + { + String type = attributes.getValue(FEATURE_TYPE); + + if (TYPE_BLOB.equals(type)) + { + byte[] id = HexUtil.hexToBytes(attributes.getValue(FEATURE_ID)); + long size = Long.parseLong(attributes.getValue(FEATURE_SIZE)); + return CDOLobUtil.createBlob(id, size); + } + + if (TYPE_CLOB.equals(type)) + { + byte[] id = HexUtil.hexToBytes(attributes.getValue(FEATURE_ID)); + long size = Long.parseLong(attributes.getValue(FEATURE_SIZE)); + return CDOLobUtil.createClob(id, size); + } + + if (TYPE_FEATURE_MAP.equals(type)) + { + String innerFeatureName = attributes.getValue(FEATURE_INNER_FEATURE); + EStructuralFeature innerFeature = revision.getEClass().getEStructuralFeature(innerFeatureName); + + String innerType = attributes.getValue(FEATURE_INNER_TYPE); + Object innerValue = value(attributes, innerType); + + return CDORevisionUtil.createFeatureMapEntry(innerFeature, innerValue); + } + + return value(attributes, type); + } + + protected Object value(Attributes attributes, String type) + { + String str = attributes.getValue(FEATURE_VALUE); + if (str == null) + { + return null; + } + + if (type == null || String.class.getSimpleName().equals(type)) + { + return str; + } + + if (Object.class.getSimpleName().equals(type)) + { + return id(str); + } + + if (Boolean.class.getSimpleName().equals(type)) + { + return Boolean.valueOf(str); + } + + if (Character.class.getSimpleName().equals(type)) + { + return str.charAt(0); + } + + if (Byte.class.getSimpleName().equals(type)) + { + return Byte.valueOf(str); + } + + if (Short.class.getSimpleName().equals(type)) + { + return Short.valueOf(str); + } + + if (Integer.class.getSimpleName().equals(type)) + { + return Integer.valueOf(str); + } + + if (Long.class.getSimpleName().equals(type)) + { + return Long.valueOf(str); + } + + if (Float.class.getSimpleName().equals(type)) + { + return Float.valueOf(str); + } + + if (Double.class.getSimpleName().equals(type)) + { + return Double.valueOf(str); + } + + if (Date.class.getSimpleName().equals(type)) + { + return new Date(Long.valueOf(str)); + } + + if (BigDecimal.class.getSimpleName().equals(type)) + { + return new BigDecimal(str); + } + + if (BigInteger.class.getSimpleName().equals(type)) + { + return new BigInteger(str); + } + + throw new IllegalArgumentException("Invalid type: " + type); + } + } + } +} |