blob: 24d9be07c6d39467502480335b8fd21dfeab5a04 [file] [log] [blame]
/*******************************************************************************
* Copyright 2011 Chair for Applied Software Engineering,
* Technische Universitaet Muenchen.
* 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:
* Florian Pirchner, Maximilian Koegel - initial API and implementation
******************************************************************************/
package org.eclipse.emf.emfstore.example.helloworld;
import java.io.IOException;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.emf.emfstore.bowling.BowlingFactory;
import org.eclipse.emf.emfstore.bowling.League;
import org.eclipse.emf.emfstore.bowling.Player;
import org.eclipse.emf.emfstore.client.ESLocalProject;
import org.eclipse.emf.emfstore.client.ESRemoteProject;
import org.eclipse.emf.emfstore.client.ESServer;
import org.eclipse.emf.emfstore.client.ESUsersession;
import org.eclipse.emf.emfstore.client.ESWorkspace;
import org.eclipse.emf.emfstore.client.ESWorkspaceProvider;
import org.eclipse.emf.emfstore.client.exceptions.ESServerNotFoundException;
import org.eclipse.emf.emfstore.client.exceptions.ESServerStartFailedException;
import org.eclipse.emf.emfstore.common.ESSystemOutProgressMonitor;
import org.eclipse.emf.emfstore.server.exceptions.ESException;
import org.eclipse.equinox.app.IApplication;
import org.eclipse.equinox.app.IApplicationContext;
/**
* An application that runs the demo.<br>
* Run a client and local server that demo the basic features of EMFStore.
*/
public class Application implements IApplication {
/**
* {@inheritDoc}
*/
public Object start(IApplicationContext context) {
try {
// Create a client representation for a local server and start a local server.
final ESServer localServer = ESServer.FACTORY.createAndStartLocalServer();
// Run a client on the local server that shows the basic features of the EMFstore
runClient(localServer);
} catch (final ESServerStartFailedException e) {
System.out.println("Server start failed!"); //$NON-NLS-1$
e.printStackTrace();
} catch (final ESException e) {
// If there is a problem with the connection to the server,
// e.g., a network, a specific EMFStoreException will be thrown.
System.out.println("Connection to Server failed!"); //$NON-NLS-1$
e.printStackTrace();
}
return IApplication.EXIT_OK;
}
/**
* Run an EMFStore Client connecting to the given server.
*
* @param server the server
* @throws ESException if the server connection fails
*/
public static void runClient(ESServer server) throws ESException {
System.out.println("Client starting..."); //$NON-NLS-1$
// The workspace is the core controller to access local and remote projects.
// A project is a container for models and their elements (EObjects).
// To get started, we obtain the current workspace of the client.
final ESWorkspace workspace = ESWorkspaceProvider.INSTANCE.getWorkspace();
// The workspace stores all available servers that have been configured. We add the local server that has
// already
// been started on the workspace.
workspace.addServer(server);
// Next, we remove all other existing servers
for (final ESServer existingServer : workspace.getServers()) {
if (existingServer != server) {
try {
workspace.removeServer(existingServer);
} catch (final ESServerNotFoundException e) {
e.printStackTrace();
}
}
}
// The workspace also contains a list of local projects that have either been created locally or checked out
// from a server.
// We create a new local project. The project new created is not yet shared with the server.
final ESLocalProject demoProject = workspace.createLocalProject("DemoProject"); //$NON-NLS-1$
// We delete all projects from the local workspace other than the one just created.
for (final ESLocalProject existingLocalProject : workspace.getLocalProjects()) {
if (existingLocalProject != demoProject) {
try {
existingLocalProject.delete(new ESSystemOutProgressMonitor());
} catch (final IOException e) {
e.printStackTrace();
}
}
}
// Next, we create a user session by logging in to the local EMFStore server with default super user
// credentials.
final ESUsersession usersession = server.login("super", "super"); //$NON-NLS-1$ //$NON-NLS-2$
// Now we can share the created local project to our server.
final ESRemoteProject remoteDemoProject = demoProject.shareProject(usersession,
new ESSystemOutProgressMonitor());
// We also retrieve a list of existing (and accessible) remote projects on the server.
// Remote projects represent a project that is currently available on the server.
// We delete all remote projects to clean up remaining projects from previous launches.
for (final ESRemoteProject existingRemoteProject : server.getRemoteProjects(usersession)) {
if (!existingRemoteProject.getGlobalProjectId().equals(remoteDemoProject.getGlobalProjectId())) {
existingRemoteProject.delete(usersession, new NullProgressMonitor());
}
}
// Now we are all set: we have a client workspace with one server configured and exactly one project shared to a
// server with only this one project.
// We check out a second, independent copy of the project (simulating a second client).
final ESLocalProject demoProjectCopy = demoProject.getRemoteProject().checkout("DemoProject Copy", //$NON-NLS-1$
usersession, new ESSystemOutProgressMonitor());
// We start working now with the local project and later we will synchronize it with the copy of the project we
// just checked out.
// We create some EObjects and add them to the project, that is, to project�s containment tree. Everything that
// is
// in the project�s containment tree (spanning tree on containment references) is considered part of the
// project. We will use an example model about bowling.
// First we add a league and set the league name.
final League league = BowlingFactory.eINSTANCE.createLeague();
league.setName("Superbowling League"); //$NON-NLS-1$
// Next we add the league to the root of the project. The project has a containment feature called model
// element that holds all root elements of a project. This list is comparable to the content list in EMF
// Resources that
// you can retrieve with getContents(). Adding something to the list will add it to the project.
demoProject.getModelElements().add(league);
// Then we create two players.
final Player player1 = BowlingFactory.eINSTANCE.createPlayer();
player1.setName("Maximilian"); //$NON-NLS-1$
final Player player2 = BowlingFactory.eINSTANCE.createPlayer();
player2.setName("Ottgar"); //$NON-NLS-1$
// Finally, we add the players to the league. Since the league is already part of the project and League.players
// is a containment feature, the players also become part of the project.
league.getPlayers().add(player1);
league.getPlayers().add(player2);
// To synchronize the local changes of the client with the server, we will commit the project.
demoProject.commit("My message", null, //$NON-NLS-1$
new ESSystemOutProgressMonitor());
// The server is now up-to-date, but we still need to synchronize the copy of the project we checked out
// earlier.
demoProjectCopy.update(new ESSystemOutProgressMonitor());
// We will now retrieve the copy of the league from the copy of the project and assert its name and player count
// are equal with the name of the project�s league.
League leagueCopy = (League) demoProjectCopy.getModelElements().get(0);
if (league.getName().equals(leagueCopy.getName()) &&
league.getPlayers().size() == leagueCopy.getPlayers().size()) {
System.out.println("Leagues names and player count are equal."); //$NON-NLS-1$
}
// Of course, we can also change something in the project copy and synchronize it back to the project.
// We change the league name to correct the type and then commit and update accordingly.
// This time, we use the IDs assigned to every EObject of a project to identify the copy of league in the
// project�s copy.
leagueCopy = (League) demoProjectCopy.getModelElement(demoProject.getModelElementId(league));
league.setName("Superbowling League"); //$NON-NLS-1$
demoProject.commit(new ESSystemOutProgressMonitor());
demoProjectCopy.update(new ESSystemOutProgressMonitor());
if (league.getName().equals(leagueCopy.getName()) &&
league.getPlayers().size() == leagueCopy.getPlayers().size()) {
System.out.println("Leagues names and player count are still equal."); //$NON-NLS-1$
}
}
/**
* {@inheritDoc}
*/
public void stop() {
}
}