- if (uri != null && !uri.trim().equals("")) {// remote
- Map<String, String> params = new HashMap<String, String>();
- params.put(
- org.apache.jackrabbit.commons.JcrUtils.REPOSITORY_URI,
- uri);
- repository = new Jcr2davRepositoryFactory()
- .getRepository(params);
- if (repository == null)
- throw new ArgeoException("Remote Davex repository " + uri
- + " not found");
- double duration = ((double) (System.currentTimeMillis() - begin)) / 1000;
- log.info("Created Jackrabbit repository in " + duration
- + " s from URI " + uri);
- // we assume that the data model of the remote repository has
- // been properly initialized
- } else {// local
- // force uri to null in order to optimize isRemote()
- uri = null;
-
- // temporary
- if (inMemory && getHomeDirectory().exists()) {
- FileUtils.deleteDirectory(getHomeDirectory());
- log.warn("Deleted Jackrabbit home directory "
- + getHomeDirectory());
+ session = login(adminCredentials);
+ // register namespaces
+ if (namespaces.size() > 0) {
+ NamespaceHelper namespaceHelper = new NamespaceHelper(session);
+ namespaceHelper.registerNamespaces(namespaces);
+ }
+
+ // load CND files from classpath or as URL
+ for (String resUrl : cndFiles) {
+ URL url;
+ Bundle dataModelBundle = null;
+
+ {// TODO put this in a separate method
+ boolean classpath;
+ // normalize URL
+ if (bundleContext != null
+ && resUrl.startsWith("classpath:")) {
+ resUrl = resUrl.substring("classpath:".length());
+ classpath = true;
+ } else if (resUrl.indexOf(':') < 0) {
+ if (!resUrl.startsWith("/")) {
+ resUrl = "/" + resUrl;
+ log.warn("Classpath should start with '/'");
+ }
+ // resUrl = "classpath:" + resUrl;
+ classpath = true;
+ } else {
+ classpath = false;
+ }
+
+ if (classpath) {
+ if (bundleContext != null) {
+ Bundle currentBundle = bundleContext.getBundle();
+ url = currentBundle.getResource(resUrl);
+ if (url != null) {// found
+ dataModelBundle = findDataModelBundle(resUrl);
+ }
+ } else {
+ resUrl = "classpath:" + resUrl;
+ url = null;
+ // url =
+ // getClass().getClassLoader().getResource(resUrl);
+ // if (url == null)
+ // url = Thread.currentThread()
+ // .getContextClassLoader()
+ // .getResource(resUrl);
+ }
+ } else if (!resUrl.startsWith("classpath:")) {
+ url = new URL(resUrl);
+ } else {
+ url = null;
+ }
+ }
+ // check existing data model nodes
+ new NamespaceHelper(session).registerNamespace(
+ ArgeoNames.ARGEO, ArgeoNames.ARGEO_NAMESPACE);
+ if (!session
+ .itemExists(ArgeoJcrConstants.DATA_MODELS_BASE_PATH))
+ JcrUtils.mkdirs(session,
+ ArgeoJcrConstants.DATA_MODELS_BASE_PATH);
+ Node dataModels = session
+ .getNode(ArgeoJcrConstants.DATA_MODELS_BASE_PATH);
+ NodeIterator it = dataModels.getNodes();
+ Node dataModel = null;
+ while (it.hasNext()) {
+ Node node = it.nextNode();
+ if (node.getProperty(ArgeoNames.ARGEO_URI).getString()
+ .equals(resUrl)) {
+ dataModel = node;
+ break;
+ }
+ }
+
+ // does nothing if data model already registered
+ if (dataModel != null && !forceCndImport) {
+ if (dataModelBundle != null) {
+ String version = dataModel.getProperty(
+ ArgeoNames.ARGEO_DATA_MODEL_VERSION)
+ .getString();
+ String dataModelBundleVersion = dataModelBundle
+ .getVersion().toString();
+ if (!version.equals(dataModelBundleVersion)) {
+ log.warn("Data model with version "
+ + dataModelBundleVersion
+ + " available, current version is "
+ + version);
+ }
+ }
+ // do not implicitly update
+ return;
+ }
+
+ InputStream in = null;
+ Reader reader = null;
+ try {
+ if (url != null) {
+ in = url.openStream();
+ } else if (resourceLoader != null) {
+ Resource res = resourceLoader.getResource(resUrl);
+ in = res.getInputStream();
+ url = res.getURL();
+ } else {
+ throw new ArgeoException("No " + resUrl
+ + " in the classpath,"
+ + " make sure the containing"
+ + " package is visible.");
+ }
+
+ reader = new InputStreamReader(in);
+ // actually imports the CND
+ CndImporter.registerNodeTypes(reader, session, true);
+
+ // FIXME: what if argeo.cnd would not be the first called on
+ // a new repo? argeo:dataModel would not be found
+ String fileName = FilenameUtils.getName(url.getPath());
+ if (dataModel == null) {
+ dataModel = dataModels.addNode(fileName);
+ dataModel.addMixin(ArgeoTypes.ARGEO_DATA_MODEL);
+ dataModel.setProperty(ArgeoNames.ARGEO_URI, resUrl);
+ } else {
+ session.getWorkspace().getVersionManager()
+ .checkout(dataModel.getPath());
+ }
+ if (dataModelBundle != null)
+ dataModel.setProperty(
+ ArgeoNames.ARGEO_DATA_MODEL_VERSION,
+ dataModelBundle.getVersion().toString());
+ else
+ dataModel.setProperty(
+ ArgeoNames.ARGEO_DATA_MODEL_VERSION, "0.0.0");
+ JcrUtils.updateLastModified(dataModel);
+ session.save();
+ session.getWorkspace().getVersionManager()
+ .checkin(dataModel.getPath());
+ } finally {
+ IOUtils.closeQuietly(in);
+ IOUtils.closeQuietly(reader);