Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 84bbe345c91514350d169352e5718985ecc5715b (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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
/*
 * Copyright (c) 2010-2013, 2016, 2018 Eike Stepper (Loehne, 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
 *    Christian W. Damus (CEA LIST) - bug 420540
 */
package org.eclipse.emf.cdo.internal.server.bundle;

import org.eclipse.emf.cdo.common.lock.IDurableLockingManager;
import org.eclipse.emf.cdo.common.util.CDOCommonUtil;
import org.eclipse.emf.cdo.server.CDOServerExporter;
import org.eclipse.emf.cdo.server.CDOServerImporter;
import org.eclipse.emf.cdo.server.CDOServerUtil;
import org.eclipse.emf.cdo.server.IRepository;
import org.eclipse.emf.cdo.server.IStoreAccessor;
import org.eclipse.emf.cdo.spi.common.branch.InternalCDOBranch;
import org.eclipse.emf.cdo.spi.common.model.InternalCDOPackageInfo;
import org.eclipse.emf.cdo.spi.common.model.InternalCDOPackageRegistry;
import org.eclipse.emf.cdo.spi.common.model.InternalCDOPackageUnit;
import org.eclipse.emf.cdo.spi.server.CDOCommand;
import org.eclipse.emf.cdo.spi.server.CDOCommand.CommandException;
import org.eclipse.emf.cdo.spi.server.InternalRepository;
import org.eclipse.emf.cdo.spi.server.InternalSession;
import org.eclipse.emf.cdo.spi.server.InternalSessionManager;
import org.eclipse.emf.cdo.spi.server.InternalView;
import org.eclipse.emf.cdo.spi.server.RepositoryConfigurator;
import org.eclipse.emf.cdo.spi.server.RepositoryFactory;

import org.eclipse.net4j.util.container.IManagedContainer;
import org.eclipse.net4j.util.container.IPluginContainer;
import org.eclipse.net4j.util.io.IOUtil;
import org.eclipse.net4j.util.lifecycle.LifecycleUtil;

import org.eclipse.osgi.framework.console.CommandInterpreter;
import org.eclipse.osgi.framework.console.CommandProvider;

import org.osgi.framework.BundleContext;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Eike Stepper
 */
public class CDOCommandProvider implements CommandProvider
{
  private static final String NEW_LINE = "\r\n"; //$NON-NLS-1$

  private static final CDOCommand list = new CDOCommand("list", "list all active repositories")
  {
    @Override
    public void execute(String[] args) throws Exception
    {
      IManagedContainer container = CDOServerApplication.getContainer();
      for (Object element : container.getElements(RepositoryFactory.PRODUCT_GROUP))
      {
        if (element instanceof InternalRepository)
        {
          InternalRepository repository = (InternalRepository)element;
          println(repository.getName());
        }
      }
    }
  };

  private static final CDOCommand start = new CDOCommand("start", "start repositories from a config file", CDOCommand.parameter("config-file"))
  {
    @Override
    public void execute(String[] args) throws Exception
    {
      String configFile = args[0];

      IManagedContainer container = CDOServerApplication.getContainer();
      RepositoryConfigurator repositoryConfigurator = new RepositoryConfigurator(container);
      IRepository[] repositories = repositoryConfigurator.configure(new File(configFile));

      println("Repositories started:");
      if (repositories != null)
      {
        for (IRepository repository : repositories)
        {
          println(repository.getName());
        }
      }
    }
  };

  private static final CDOCommand stop = new CDOCommand.WithRepository("stop", "stop a repository")
  {
    @Override
    public void execute(InternalRepository repository, String[] args) throws Exception
    {
      LifecycleUtil.deactivate(repository);
      println("Repository stopped");
    }
  };

  private static final CDOCommand exportXML = new CDOCommand.WithRepository("export", "export the contents of a repository to an XML file",
      CDOCommand.parameter("export-file"))
  {
    @Override
    public void execute(InternalRepository repository, String[] args) throws Exception
    {
      String exportFile = args[0];
      OutputStream out = null;

      try
      {
        out = new FileOutputStream(exportFile);

        CDOServerExporter.XML exporter = new CDOServerExporter.XML(repository);
        exporter.exportRepository(out);

        if (args.length > 1)
        {
          if ("withSystemPackages".equalsIgnoreCase(args[1]))
          {
            exporter.setExportSystemPackages(true);
          }
        }

        println("Repository exported");
      }
      finally
      {
        IOUtil.close(out);
      }
    }
  };

  private static final CDOCommand importXML = new CDOCommand.WithRepository("import", "import the contents of a repository from an XML file",
      CDOCommand.parameter("import-file"))
  {
    @Override
    public void execute(InternalRepository repository, String[] args) throws Exception
    {
      String importFile = args[0];
      InputStream in = null;

      try
      {
        in = new FileInputStream(importFile);
        LifecycleUtil.deactivate(repository);

        CDOServerImporter.XML importer = new CDOServerImporter.XML(repository);
        importer.importRepository(in);

        IManagedContainer container = CDOServerApplication.getContainer();
        CDOServerUtil.addRepository(container, repository);

        println("Repository imported");
      }
      finally
      {
        IOUtil.close(in);
      }
    }
  };

  private static final CDOCommand branches = new CDOCommand.WithRepository("branches", "dump the branches of a repository")
  {
    @Override
    public void execute(InternalRepository repository, String[] args) throws Exception
    {
      branches(repository.getBranchManager().getMainBranch(), "");
    }

    private void branches(InternalCDOBranch branch, String prefix)
    {
      println(prefix + branch);
      prefix += CDOCommand.INDENT;
      for (InternalCDOBranch child : branch.getBranches())
      {
        branches(child, prefix);
      }
    }
  };

  private static final CDOCommand packages = new CDOCommand.WithRepository("packages", "dump the packages of a repository")
  {
    @Override
    public void execute(InternalRepository repository, String[] args) throws Exception
    {
      InternalCDOPackageRegistry packageRegistry = repository.getPackageRegistry(false);
      for (InternalCDOPackageUnit packageUnit : packageRegistry.getPackageUnits())
      {
        println(packageUnit);
        for (InternalCDOPackageInfo packageInfo : packageUnit.getPackageInfos())
        {
          println(CDOCommand.INDENT + packageInfo);
        }
      }
    }
  };

  private static final CDOCommand sessions = new CDOCommand.WithRepository("sessions", "dump the sessions of a repository")
  {
    @Override
    public void execute(InternalRepository repository, String[] args) throws Exception
    {
      InternalSessionManager sessionManager = repository.getSessionManager();
      for (InternalSession session : sessionManager.getSessions())
      {
        println(session);
        for (InternalView view : session.getViews())
        {
          println(INDENT + view);
        }
      }
    }
  };

  private static final CDOCommand locks = new CDOCommand.WithAccessor("locks", "dump the locks of a repository", CDOCommand.optional("username-prefix"))
  {
    @Override
    public void execute(InternalRepository repository, IStoreAccessor accessor, String[] args) throws Exception
    {
      String usernamePrefix = args[0];

      repository.getLockingManager().getLockAreas(usernamePrefix, new IDurableLockingManager.LockArea.Handler()
      {
        public boolean handleLockArea(IDurableLockingManager.LockArea area)
        {
          println(area.getDurableLockingID());
          println(CDOCommand.INDENT + "userID = " + area.getUserID());
          println(CDOCommand.INDENT + "branch = " + area.getBranch());
          println(CDOCommand.INDENT + "timeStamp = " + CDOCommonUtil.formatTimeStamp(area.getTimeStamp()));
          println(CDOCommand.INDENT + "readOnly = " + area.isReadOnly());
          println(CDOCommand.INDENT + "locks = " + area.getLocks());
          return true;
        }
      });
    }
  };

  private static final CDOCommand deletelocks = new CDOCommand.WithAccessor("deletelocks", "delete a durable locking area of a repository",
      CDOCommand.parameter("area-id"))
  {
    @Override
    public void execute(InternalRepository repository, IStoreAccessor accessor, String[] args) throws Exception
    {
      String areaID = args[0];
      repository.getLockingManager().deleteLockArea(areaID);
    }
  };

  public CDOCommandProvider(BundleContext bundleContext)
  {
    bundleContext.registerService(CommandProvider.class.getName(), this, null);
  }

  public Object _cdo(CommandInterpreter interpreter)
  {
    try
    {
      Map<String, CDOCommand> commands = getCommands();
      String cmd = interpreter.nextArgument();

      CDOCommand command = commands.get(cmd);
      if (command != null)
      {
        try
        {
          command.setInterpreter(interpreter);
          command.execute();
          return null;
        }
        finally
        {
          command.setInterpreter(null);
        }
      }

      interpreter.println(getHelp());
    }
    catch (CommandException ex)
    {
      interpreter.println(ex.getMessage());
    }
    catch (Exception ex)
    {
      interpreter.printStackTrace(ex);
    }

    return null;
  }

  public String getHelp()
  {
    StringBuilder builder = new StringBuilder();

    try
    {
      builder.append("---CDO commands---" + NEW_LINE);

      List<CDOCommand> commands = new ArrayList<CDOCommand>(getCommands().values());
      Collections.sort(commands, new Comparator<CDOCommand>()
      {
        public int compare(CDOCommand o1, CDOCommand o2)
        {
          return o1.getName().compareTo(o2.getName());
        }
      });

      for (CDOCommand command : commands)
      {
        try
        {
          builder.append(CDOCommand.INDENT);
          builder.append(command.getSyntax());
          builder.append(" - ");
          builder.append(command.getDescription());
          builder.append(NEW_LINE);
        }
        catch (Exception ex)
        {
          OM.LOG.error(ex);
        }
      }
    }
    catch (Exception ex)
    {
      OM.LOG.error(ex);
    }

    return builder.toString();
  }

  public synchronized Map<String, CDOCommand> getCommands()
  {
    Map<String, CDOCommand> commands = new HashMap<String, CDOCommand>();
    addCommand(commands, list);
    addCommand(commands, start);
    addCommand(commands, stop);
    addCommand(commands, exportXML);
    addCommand(commands, importXML);
    addCommand(commands, branches);
    addCommand(commands, deletelocks);
    addCommand(commands, locks);
    addCommand(commands, packages);
    addCommand(commands, sessions);

    try
    {
      for (String name : IPluginContainer.INSTANCE.getFactoryTypes(CDOCommand.PRODUCT_GROUP))
      {
        try
        {
          CDOCommand command = createCommand(name);
          addCommand(commands, command);
        }
        catch (Exception ex)
        {
          OM.LOG.error(ex);
        }
      }
    }
    catch (Exception ex)
    {
      OM.LOG.error(ex);
    }

    return commands;
  }

  protected CDOCommand createCommand(String name)
  {
    return (CDOCommand)IPluginContainer.INSTANCE.getElement(CDOCommand.PRODUCT_GROUP, name, null);
  }

  private void addCommand(Map<String, CDOCommand> commands, CDOCommand command)
  {
    commands.put(command.getName(), command);
  }
}

Back to the top