Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: cd0cd2e002e194d55f962f789f5ef4969a16fd65 (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
196
197
/*******************************************************************************
 * Copyright (c) 2004 Peter Nehrer and Composent, Inc.
 * 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:
 *     Peter Nehrer - initial API and implementation
 *******************************************************************************/
package org.eclipse.ecf.example.sdo.editor;

import java.util.MissingResourceException;
import java.util.ResourceBundle;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Status;
import org.eclipse.ecf.core.ISharedObjectContainer;
import org.eclipse.ecf.core.identity.ID;
import org.eclipse.ecf.core.identity.IDFactory;
import org.eclipse.ecf.core.util.ECFException;
import org.eclipse.ecf.example.collab.Client;
import org.eclipse.ecf.sdo.DataGraphSharingFactory;
import org.eclipse.ecf.sdo.ISharedDataGraph;
import org.eclipse.ecf.sdo.IUpdateConsumer;
import org.eclipse.ecf.sdo.WaitablePublicationCallback;
import org.eclipse.ecf.sdo.WaitableSubscriptionCallback;
import org.eclipse.ecf.sdo.emf.EMFUpdateProvider;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.osgi.framework.BundleContext;

import commonj.sdo.DataGraph;

/**
 * The main plugin class to be used in the desktop.
 * 
 * @author pnehrer
 */
public class EditorPlugin extends AbstractUIPlugin {
    // The shared instance.
    private static EditorPlugin plugin;

    // Resource bundle.
    private ResourceBundle resourceBundle;

    private Client client;

    /**
     * The constructor.
     */
    public EditorPlugin() {
        super();
        plugin = this;
        try {
            resourceBundle = ResourceBundle
                    .getBundle("org.eclipse.ecf.example.sdo.editor.EditorPluginResources");
        } catch (MissingResourceException x) {
            resourceBundle = null;
        }
    }

    /**
     * This method is called upon plug-in activation
     */
    public void start(BundleContext context) throws Exception {
        super.start(context);
    }

    /**
     * This method is called when the plug-in is stopped
     */
    public void stop(BundleContext context) throws Exception {
        super.stop(context);
    }

    /**
     * Returns the shared instance.
     */
    public static EditorPlugin getDefault() {
        return plugin;
    }

    /**
     * Returns the string from the plugin's resource bundle, or 'key' if not
     * found.
     */
    public static String getResourceString(String key) {
        ResourceBundle bundle = EditorPlugin.getDefault().getResourceBundle();
        try {
            return (bundle != null) ? bundle.getString(key) : key;
        } catch (MissingResourceException e) {
            return key;
        }
    }

    /**
     * Returns the plugin's resource bundle,
     */
    public ResourceBundle getResourceBundle() {
        return resourceBundle;
    }

    public void log(Throwable t) {
        if (t instanceof CoreException)
            getLog().log(((CoreException) t).getStatus());
        else
            getLog().log(
                    new Status(Status.ERROR, getBundle().getSymbolicName(), 0,
                            "An unexpected error occurred.", t));
    }

    public synchronized ISharedDataGraph subscribe(String path,
            IUpdateConsumer consumer) throws ECFException {
        Path p = new Path(path);
        ISharedObjectContainer container = getContainer(ResourcesPlugin
                .getWorkspace().getRoot().getProject(p.segment(0)));
        PublishedGraphTracker tracker = getTracker(container);

        ID id = IDFactory.makeStringID(path);
        WaitableSubscriptionCallback mutex = new WaitableSubscriptionCallback();
        ISharedDataGraph result = DataGraphSharingFactory.getDataGraphSharing(
                container, "default").subscribe(id, new EMFUpdateProvider(),
                consumer, mutex);
        ID containerID = null;
        try {
            containerID = mutex.waitForSubscription(5000);
        } catch (InterruptedException e) {
            throw new ECFException(e);
        }

        if (containerID == null)
            throw new ECFException("Subscription timed out.");

        tracker.add(id);
        return result;
    }

    public synchronized ISharedDataGraph publish(String path,
            DataGraph dataGraph, IUpdateConsumer consumer) throws ECFException {
        Path p = new Path(path);
        ISharedObjectContainer container = getContainer(ResourcesPlugin
                .getWorkspace().getRoot().getProject(p.segment(0)));
        PublishedGraphTracker tracker = getTracker(container);

        ID id = IDFactory.makeStringID(path);
        WaitablePublicationCallback mutex = new WaitablePublicationCallback();
        ISharedDataGraph result = DataGraphSharingFactory.getDataGraphSharing(
                container, "default").publish(dataGraph, id,
                new EMFUpdateProvider(), consumer, mutex);
        try {
            if (!mutex.waitForPublication(5000))
                throw new ECFException("Publication timed out.");
        } catch (InterruptedException e) {
            throw new ECFException(e);
        }

        tracker.add(id);
        return result;
    }

    public synchronized boolean isPublished(String path) throws ECFException {
        Path p = new Path(path);
        ISharedObjectContainer container = getContainer(ResourcesPlugin
                .getWorkspace().getRoot().getProject(p.segment(0)));
        PublishedGraphTracker tracker = getTracker(container);
        return tracker.isPublished(IDFactory.makeStringID(path));
    }

    public synchronized void checkConnected(IProject project)
            throws ECFException {
        if (getContainer(project) == null)
            throw new ECFException("Project " + project.getName()
                    + " is not connected.");
    }

    private ISharedObjectContainer getContainer(IProject project)
            throws ECFException {
        return Client.getContainer(project);
    }

    private PublishedGraphTracker getTracker(ISharedObjectContainer container)
            throws ECFException {
        ID id = IDFactory.makeStringID(PublishedGraphTracker.class.getName());
        PublishedGraphTracker tracker = (PublishedGraphTracker) container
                .getSharedObjectManager().getSharedObject(id);
        if (tracker == null) {
            tracker = new PublishedGraphTracker();
            container.getSharedObjectManager().addSharedObject(id, tracker,
                    null, null);
        }

        return tracker;
    }
}

Back to the top