Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 19b3645c578ae37dfb1a254b18e588410d7c9700 (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
/***************************************************************************
 * Copyright (c) 2004 - 2009 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.internal.cdo.net4j;

import org.eclipse.emf.internal.cdo.messages.Messages;

import org.eclipse.net4j.connector.IConnector;
import org.eclipse.net4j.signal.failover.NOOPFailOverStrategy;
import org.eclipse.net4j.util.StringUtil;
import org.eclipse.net4j.util.container.IElementProcessor;
import org.eclipse.net4j.util.container.IManagedContainer;

import org.eclipse.spi.net4j.ConnectorFactory;

import java.text.MessageFormat;

/**
 * @author Eike Stepper
 */
public class FailOverStrategyInjector implements IElementProcessor
{
  private static final String INVALID_URI_MESSAGE = Messages.getString("InvalidURIException.0"); //$NON-NLS-1$

  private static final String SCHEME_SEPARATOR = "://"; //$NON-NLS-1$

  public FailOverStrategyInjector()
  {
  }

  public Object process(IManagedContainer container, String productGroup, String factoryType, String description,
      Object element)
  {
    if (element instanceof CDONet4jSessionImpl)
    {
      CDONet4jSessionImpl session = (CDONet4jSessionImpl)element;
      NOOPFailOverStrategy failOverStrategy = getFailOverStrategy(container, description);
      session.setFailOverStrategy(failOverStrategy);
    }

    return element;
  }

  protected NOOPFailOverStrategy getFailOverStrategy(IManagedContainer container, String description)
  {
    IConnector connector = getConnector(container, description);
    return new NOOPFailOverStrategy(connector);
  }

  protected IConnector getConnector(IManagedContainer container, String description)
  {
    int pos = description.indexOf(SCHEME_SEPARATOR);
    if (pos == -1)
    {
      throw new IllegalArgumentException(MessageFormat.format(INVALID_URI_MESSAGE, description, Messages
          .getString("FailOverStrategyInjector.0"))); //$NON-NLS-1$
    }

    String factoryType = description.substring(0, pos);
    if (StringUtil.isEmpty(factoryType))
    {
      throw new IllegalArgumentException(MessageFormat.format(INVALID_URI_MESSAGE, description, Messages
          .getString("FailOverStrategyInjector.1"))); //$NON-NLS-1$
    }

    String connectorDescription = description.substring(pos + SCHEME_SEPARATOR.length());
    if (StringUtil.isEmpty(connectorDescription))
    {
      throw new IllegalArgumentException(MessageFormat.format(INVALID_URI_MESSAGE, description, Messages
          .getString("FailOverStrategyInjector.2"))); //$NON-NLS-1$
    }

    pos = connectorDescription.indexOf('?');
    if (pos != -1)
    {
      connectorDescription = connectorDescription.substring(0, pos);
    }

    return (IConnector)container.getElement(ConnectorFactory.PRODUCT_GROUP, factoryType, connectorDescription);
  }
}

Back to the top