/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  The ASF licenses this file to You
 * under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.jackrabbit.jcr2spi;

import org.apache.jackrabbit.test.RepositoryStub;
import org.apache.jackrabbit.test.RepositoryStubException;
import org.apache.jackrabbit.spi2dav.RepositoryServiceImpl;
import org.apache.jackrabbit.spi2dav.IdFactoryImpl;
import org.apache.jackrabbit.spi2dav.ValueFactoryImpl;
import org.apache.jackrabbit.jcr2spi.config.RepositoryConfig;
import org.apache.jackrabbit.spi.RepositoryService;
import org.apache.jackrabbit.spi.IdFactory;
import org.apache.log4j.PropertyConfigurator;

import javax.jcr.Repository;
import javax.jcr.ValueFactory;
import javax.jcr.RepositoryException;
import java.util.Properties;

/**
 * <code>JCR2SPIRepositoryStub</code> implements a repository stub that returns
 * a JCR2SPI Client that works on a SPI2DAV implementation which in turn
 * connects to an JCR WebDAV Server.
 */
public class JCR2SPIRepositoryStub extends RepositoryStub {

    /**
     * Property for the repository url
     */
    public static final String PROP_REPOSITORY_URL = "org.apache.jackrabbit.jcr2spi.repository.url";
    /**
     * Property for the default workspace name
     */
    public static final String PROP_WORKSPACE_NAME = "org.apache.jackrabbit.jcr2spi.workspace.name";

    static {
        PropertyConfigurator.configure(JCR2SPIRepositoryStub.class.getClassLoader().getResource("log4j.properties"));
    }

    /**
     * The repository instance
     */
    private Repository repository;

    /**
     * Overwritten constructor from base class.
     */
    public JCR2SPIRepositoryStub(Properties env) {
        super(env);
    }

    public synchronized Repository getRepository() throws RepositoryStubException {
        if (repository == null) {
            try {
                String url = environment.getProperty(PROP_REPOSITORY_URL);

                final IdFactory idFactory = IdFactoryImpl.getInstance();
                final ValueFactory vFactory = ValueFactoryImpl.getInstance();
                final RepositoryServiceImpl webdavRepoService = new RepositoryServiceImpl(url, idFactory, vFactory);

                RepositoryConfig config = new RepositoryConfig() {
                    public RepositoryService getRepositoryService() {
                        return webdavRepoService;
                    }

                    public ValueFactory getValueFactory() throws RepositoryException {
                        return vFactory;
                    }

                    public String getDefaultWorkspaceName() {
                        String name = environment.getProperty(PROP_WORKSPACE_NAME);
                        return name;
                    }
                };

                repository = RepositoryImpl.create(config);
            } catch (Exception e) {
                throw new RepositoryStubException(e.toString());
            }
        }
        return repository;
    }
}

@@ -1065,16 +1065,78 @@
 Config;%0A
+import org.apache.jackrabbit.jcr2spi.config.CacheMaintenance;%0A
 import o
@@ -1334,46 +1334,8 @@
 ry;%0A
-import javax.jcr.RepositoryException;%0A
 impo
@@ -3055,35 +3055,8 @@
 ry()
- throws RepositoryException
  %7B%0A 
@@ -3314,32 +3314,192 @@
                %7D
+%0A%0A                    public CacheMaintenance getCacheMaintenanceStrategy() %7B%0A                        return CacheMaintenance.OBSERVATION;%0A                    %7D
 %0A               

work in progress