Skip to main content
summaryrefslogtreecommitdiffstats
blob: b7f650ff20307aa8edd591f05320bf8c8252f104 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/*
 * Copyright (c) 2010-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.workspace.efs;

import org.eclipse.emf.cdo.location.ICheckoutSource;
import org.eclipse.emf.cdo.location.IRepositoryLocation;
import org.eclipse.emf.cdo.location.IRepositoryLocationManager;
import org.eclipse.emf.cdo.server.db.CDODBUtil;
import org.eclipse.emf.cdo.server.db.IDBStore;
import org.eclipse.emf.cdo.server.db.mapping.IMappingStrategy;
import org.eclipse.emf.cdo.workspace.CDOWorkspace;
import org.eclipse.emf.cdo.workspace.CDOWorkspaceBase;
import org.eclipse.emf.cdo.workspace.CDOWorkspaceConfiguration;
import org.eclipse.emf.cdo.workspace.CDOWorkspaceUtil;
import org.eclipse.emf.cdo.workspace.internal.efs.CDOWorkspaceFileSystem;
import org.eclipse.emf.cdo.workspace.internal.efs.CDOWorkspaceStore;

import org.eclipse.net4j.db.DBUtil;
import org.eclipse.net4j.db.IDBAdapter;
import org.eclipse.net4j.db.IDBConnectionProvider;
import org.eclipse.net4j.db.h2.H2Adapter;
import org.eclipse.net4j.util.io.IOUtil;

import org.eclipse.core.filesystem.EFS;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IProjectDescription;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;

import org.h2.jdbcx.JdbcDataSource;

import javax.sql.DataSource;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URI;

/**
 * This class is the main entry point to the {@link org.eclipse.core.filesystem.EFS Eclipse File System (EFS)} implementation
 * for CDO {@link org.eclipse.emf.cdo.workspace.CDOWorkspace workspaces}.
 * @author Eike Stepper
 */
public final class CDOFS
{
  private CDOFS()
  {
  }

  public static CDOWorkspace open(String projectName, File projectFolder) throws Exception
  {
    IDBStore local = creatLocalStore(projectFolder);
    CDOWorkspaceBase base = createWorkspaceBase(new File(projectFolder, "base"));
    IRepositoryLocation remote = readRepositoryLocation(projectFolder);

    CDOWorkspaceConfiguration config = CDOWorkspaceUtil.createWorkspaceConfiguration();
    config.setStore(local);
    config.setBase(base);
    config.setRemote(remote);

    CDOWorkspace workspace = config.open();
    return workspace;
  }

  public static void checkout(ICheckoutSource checkoutSource, String projectName, IProgressMonitor monitor)
      throws Exception
  {
    IWorkspace workspace = ResourcesPlugin.getWorkspace();
    IWorkspaceRoot root = workspace.getRoot();
    IProject project = root.getProject(projectName);
    if (project.exists())
    {
      throw new IOException("Project " + projectName + " already exists");
    }

    File projectFolder = root.getLocation().append(projectName).toFile();
    URI uri = checkout(checkoutSource, projectName, projectFolder);

    IProjectDescription description = workspace.newProjectDescription(projectName);
    description.setLocationURI(uri);

    project.create(description, new NullProgressMonitor());
    if (!project.isOpen())
    {
      project.open(new NullProgressMonitor());
    }
  }

  private static URI checkout(ICheckoutSource checkoutSource, String projectName, File projectFolder) throws Exception
  {
    IDBStore local = creatLocalStore(projectFolder);
    CDOWorkspaceBase base = createWorkspaceBase(new File(projectFolder, "base"));

    IRepositoryLocation remote = checkoutSource.getRepositoryLocation();
    writeRepositoryLocation(projectFolder, remote);

    String branchPath = checkoutSource.getBranchPath();
    long timeStamp = checkoutSource.getTimeStamp();

    CDOWorkspaceConfiguration config = CDOWorkspaceUtil.createWorkspaceConfiguration();
    config.setStore(local);
    config.setBase(base);
    config.setRemote(remote);
    config.setBranchPath(branchPath);
    config.setTimeStamp(timeStamp);

    CDOWorkspace workspace = config.checkout();
    CDOWorkspaceStore store = getFileSystem().addWorkspaceStore(projectName, workspace);
    return store.toURI();
  }

  private static CDOWorkspaceFileSystem getFileSystem() throws CoreException
  {
    return (CDOWorkspaceFileSystem)EFS.getFileSystem(CDOWorkspaceFileSystem.SCHEME);
  }

  private static IDBStore creatLocalStore(File projectFolder)
  {
    IMappingStrategy mappingStrategy = CDODBUtil.createHorizontalMappingStrategy(false);
    IDBAdapter dbAdapter = createLocalAdapter();
    IDBConnectionProvider dbConnectionProvider = DBUtil.createConnectionProvider(createLocalDataSource(new File(
        projectFolder, "local")));
    IDBStore local = CDODBUtil.createStore(mappingStrategy, dbAdapter, dbConnectionProvider);
    return local;
  }

  private static IDBAdapter createLocalAdapter()
  {
    return new H2Adapter();
  }

  private static DataSource createLocalDataSource(File folder)
  {
    folder.mkdirs();
    String path = folder.getAbsolutePath().replace('\\', '/');

    JdbcDataSource dataSource = new JdbcDataSource();
    dataSource.setURL("jdbc:h2:" + path + "/db");
    return dataSource;
  }

  private static CDOWorkspaceBase createWorkspaceBase(File folder)
  {
    folder.mkdirs();
    return CDOWorkspaceUtil.createFolderWorkspaceBase(folder);
  }

  private static File getRemotePropertiesFile(File projectFolder)
  {
    return new File(projectFolder, "remote.properties");
  }

  private static void writeRepositoryLocation(File projectFolder, IRepositoryLocation remote) throws IOException
  {
    FileOutputStream out = null;

    try
    {
      out = new FileOutputStream(getRemotePropertiesFile(projectFolder));
      remote.write(out);
    }
    finally
    {
      IOUtil.close(out);
    }
  }

  private static IRepositoryLocation readRepositoryLocation(File projectFolder) throws IOException
  {
    FileInputStream in = null;

    try
    {
      in = new FileInputStream(getRemotePropertiesFile(projectFolder));
      return IRepositoryLocationManager.INSTANCE.addRepositoryLocation(in);
    }
    finally
    {
      IOUtil.close(in);
    }
  }
}

Back to the top