Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 7885fda2b3ce6cdf5683e235dbc20648ee784d6a (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
/*
 * Copyright (c) 2015 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:
 *    Victor Roldan Betancort - initial API and implementation
 */
package org.eclipse.emf.cdo.doc.programmers.client;

import org.eclipse.emf.cdo.eresource.CDOResourceFactory;
import org.eclipse.emf.cdo.net4j.CDONet4jSessionConfiguration;
import org.eclipse.emf.cdo.net4j.CDONet4jUtil;
import org.eclipse.emf.cdo.session.CDOSession;
import org.eclipse.emf.cdo.transaction.CDOTransaction;
import org.eclipse.emf.cdo.view.AbstractCDOViewProvider;
import org.eclipse.emf.cdo.view.CDOView;
import org.eclipse.emf.cdo.view.CDOViewProvider;
import org.eclipse.emf.cdo.view.CDOViewProviderRegistry;

import org.eclipse.net4j.Net4jUtil;
import org.eclipse.net4j.connector.IConnector;
import org.eclipse.net4j.tcp.TCPUtil;
import org.eclipse.net4j.util.container.IManagedContainer;
import org.eclipse.net4j.util.container.ManagedContainer;

import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;

/**
 * View Providers
 * <p>
 * A {@link CDOViewProvider view provider} allows clients to inject custom logic into the {@link CDOResourceFactory
 * resource factory} mechansim, capable of handling the whole {@link CDOSession session} and {@link CDOView view}
 * instantiation process. This permits to obtain {@link Resource resources} through the {@link ResourceSet resource set}
 * API transparently, without any prior CDO client API code. The view provider automatically kicks in the middle of the
 * {@link ResourceSet#getResource(org.eclipse.emf.common.util.URI, boolean) ResourceSet.getResource()} call, forgetting
 * about the whole openning session / openning transaction process, which happens behind the scenes.
 * <p>
 * This is quite useful when integrating CDO with EMF-based frameworks and tools that are not prepared for a CDO
 * scenario themselves.
 * <p>
 * <b>Table of Contents</b> {@toc}
 *
 * @author Victor Roldan Betancort
 */
public class Doc03_ViewProviders
{
  /**
   * Implementing a View Provider
   * <p>
   * Clients should implement the {@link CDOViewProvider} interface, or sub class the {@link AbstractCDOViewProvider}
   * class, which provides common functionality.
   * <p>
   * The example below shows a simple implementation that opens a <b>new</b> {@link CDOSession session} to a local
   * server and a <b>new</b> {@link CDOTransaction transaction} on that session.
   * <p>
   * {@link #example() ExampleViewProvider.java}
   */
  public class ProviderImplementation
  {
    /**
     * @snippet
     * @callout The example provider catches all URIs with shape "cdo.local:<repoName>".
     * @callout Register the provider with {@link CDOViewProviderRegistry}.
     */
    public void example()
    {
      /* callout */new AbstractCDOViewProvider("cdo\\.local:.*", 100)
      {
        private IManagedContainer container;

        /* callout */public CDOView getView(URI uri, ResourceSet resourceSet)
        {
          if (container == null)
          {
            container = new ManagedContainer();
            Net4jUtil.prepareContainer(container);
            TCPUtil.prepareContainer(container);
            container.activate();
          }

          int startIndex = uri.toString().indexOf(':');
          String repoName = uri.toString().substring(startIndex);

          IConnector connector = (IConnector)container.getElement("org.eclipse.net4j.connectors", "tcp", "localhost");

          CDONet4jSessionConfiguration config = CDONet4jUtil.createNet4jSessionConfiguration();
          config.setConnector(connector);
          config.setRepositoryName(repoName);

          CDOSession session = config.openNet4jSession();
          return session.openTransaction();
        }
      };
    }
  }

  /**
   * Contributing View Providers Programmatically
   * <p>
   * A client's view provider implementation can be contributed programmatically to the {@link CDOViewProviderRegistry},
   * as the following example suggests:
   * <p>
   * {@link #snippet2() ProviderContribution.java}
   */
  public class ContributeProviderProgrammatically
  {
    /**
     * @snippet
     * @callout Get the target {@link CDOViewProvider} implementation.
     * @callout Add the provider instance to {@link CDOViewProviderRegistry}.
     */
    @SuppressWarnings("restriction")
    public void snippet2()
    {
      // Instantiate your view provider
      CDOViewProvider viewProvider = /* callout */org.eclipse.emf.internal.cdo.view.PluginContainerViewProvider.INSTANCE;

      // Add the instance to the registry
      /* callout */CDOViewProviderRegistry.INSTANCE.addViewProvider(viewProvider);
    }
  }

  /**
   * Contributing View Providers Using Extension Points
   * <p>
   * A specific {@link CDOViewProvider} implementation can also be contributed using the
   * <code>org.eclipse.emf.cdo.viewProviders</code> extension point. Clients specify:
   * <p>
   * <ul>
   * <li>A mandatory <b><code>class</code></b> implementing the {@link CDOViewProvider} interface.
   * <li>A mandatory <b>regular expression</b> string indicating the shape of {@link org.eclipse.emf.common.util.URI
   * URIs} that should match with the contributed provider.
   * <li>An optional <b><code>priority</code></b> integer value, to indicate preference over other implementations
   * matching the same regular expression. A higher value indicates a higher priority, {@link Integer#MAX_VALUE} being
   * the maximum priority value and {@link Integer#MIN_VALUE} the minimum.
   * </ul>
   */
  public class ContributeProviderUsingExtensionPoint
  {
  }
}

Back to the top