Skip to main content
summaryrefslogtreecommitdiffstats
blob: 21ba71b61cf773afe37cd963c248f8086c9bd4ca (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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/*
 * Copyright (c) 2011, 2012, 2014 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.util;

import org.eclipse.emf.cdo.CDOObject;
import org.eclipse.emf.cdo.CDOState;
import org.eclipse.emf.cdo.view.CDOObjectHandler;
import org.eclipse.emf.cdo.view.CDOView;

import org.eclipse.emf.common.notify.Notifier;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.util.EContentAdapter;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.emf.spi.cdo.FSMUtil;
import org.eclipse.emf.spi.cdo.InternalCDOView;

import java.lang.ref.WeakReference;
import java.util.HashSet;
import java.util.Set;

/**
 * A scalable {@link EContentAdapter content adapter} that uses CDO mechansims to attach itself to {@link CDOObject
 * objects} when they are lazily loaded.
 *
 * @author Victor Roldan Betancort
 * @since 4.0
 */
public class CDOLazyContentAdapter extends EContentAdapter
{
  private CDOObjectHandler handler = new CleanObjectHandler();

  private Set<WeakReference<CDOObject>> adaptedObjects = new HashSet<WeakReference<CDOObject>>();

  /**
   * The root object to be adapted.
   */
  private WeakReference<CDOObject> adaptedRoot;

  @Override
  protected void setTarget(EObject target)
  {
    if (isConnectedObject(target))
    {
      if (adaptedRoot == null)
      {
        adaptedRoot = new WeakReference<CDOObject>(CDOUtil.getCDOObject(target));
      }

      basicSetTarget(target);
      if (target instanceof Resource)
      {
        addCleanObjectHandler(target);
      }
    }
    else
    {
      super.setTarget(target);
    }
  }

  /**
   * EContentAdapter removes adapter from all contained EObjects. In this case, we remove this adapter from all lazily
   * loaded objects
   */
  @Override
  protected void unsetTarget(EObject target)
  {
    if (isConnectedObject(target))
    {
      basicUnsetTarget(target);
      if (target instanceof Resource)
      {
        InternalCDOView view = getCDOView(target);
        if (view != null)
        {
          // Remove adapter from all adapted objects
          for (WeakReference<CDOObject> weakReference : adaptedObjects)
          {
            CDOObject object = weakReference.get();
            if (object != null)
            {
              removeAdapter(object);
            }
          }
        }

        target.eAdapters().remove(this);
        removeCleanObjectHandler(target);
      }
    }
    else
    {
      super.unsetTarget(target);
    }
  }

  private void addCleanObjectHandler(EObject target)
  {
    InternalCDOView view = getCDOView(target);
    if (view != null)
    {
      CDOObjectHandler[] handlers = view.getObjectHandlers();
      for (CDOObjectHandler handler : handlers)
      {
        if (handler.equals(this.handler))
        {
          return;
        }
      }

      view.addObjectHandler(handler);

      // Adapt already loaded objects
      for (CDOObject cdoObject : view.getObjectsList())
      {
        if (isContained(cdoObject))
        {
          addAdapter(cdoObject);
        }
      }
    }
  }

  private void removeCleanObjectHandler(EObject target)
  {
    InternalCDOView view = getCDOView(target);
    if (view != null)
    {
      CDOObjectHandler[] handlers = view.getObjectHandlers();
      for (CDOObjectHandler handler : handlers)
      {
        if (handler.equals(this.handler))
        {
          view.removeObjectHandler(handler);
          break;
        }
      }
    }
  }

  @Override
  protected void addAdapter(Notifier notifier)
  {
    if (isConnectedObject(notifier) && !isAlreadyAdapted(notifier))
    {
      adaptedObjects.add(new WeakReference<CDOObject>(CDOUtil.getCDOObject((EObject)notifier)));
    }

    super.addAdapter(notifier);
  }

  private boolean isAlreadyAdapted(Notifier notifier)
  {
    return notifier.eAdapters().contains(this);
  }

  private static InternalCDOView getCDOView(EObject target)
  {
    CDOObject object = CDOUtil.getCDOObject(target);
    if (object != null)
    {
      return (InternalCDOView)object.cdoView();
    }

    return null;
  }

  private static boolean isConnectedObject(Notifier target)
  {
    if (target instanceof EObject)
    {
      CDOObject object = CDOUtil.getCDOObject((EObject)target);
      if (object != null)
      {
        return !FSMUtil.isTransient(object);
      }
    }

    return false;
  }

  /**
   * Checks if the argument is contained in the object graph of the root element
   */
  private boolean isContained(CDOObject object)
  {
    if (adaptedRoot == null)
    {
      return false;
    }

    CDOObject root = adaptedRoot.get();
    if (object == null)
    {
      return false;
    }

    if (root instanceof Resource)
    {
      return root == (object instanceof Resource ? object : object.cdoResource());
    }

    return EcoreUtil.isAncestor(root, object);
  }

  /**
   * @author Victor Roldan Betancort
   */
  private final class CleanObjectHandler implements CDOObjectHandler
  {
    public void objectStateChanged(CDOView view, CDOObject object, CDOState oldState, CDOState newState)
    {
      if (newState == CDOState.CLEAN || newState == CDOState.NEW)
      {
        if (isConnectedObject(object) && !isAlreadyAdapted(object) && isContained(object))
        {
          addAdapter(object);
        }
      }
    }
  }
}

Back to the top