View Javadoc

1   /*
2    * Copyright 2004-2008 the Seasar Foundation and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package org.seasar.eclipse.common.launch;
17  
18  import org.eclipse.core.runtime.CoreException;
19  import org.eclipse.debug.core.DebugPlugin;
20  import org.eclipse.debug.core.ILaunchConfiguration;
21  import org.eclipse.debug.core.ILaunchConfigurationType;
22  import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
23  import org.eclipse.debug.core.ILaunchManager;
24  import org.seasar.eclipse.common.CommonPlugin;
25  
26  /**
27   * @author taichi
28   * 
29   */
30  public class LaunchConfigurationFactory {
31  
32      public static ILaunchConfiguration create(CreationHandler handler) {
33          ILaunchConfiguration config = null;
34          try {
35              ILaunchManager manager = DebugPlugin.getDefault()
36                      .getLaunchManager();
37              ILaunchConfigurationType type = manager
38                      .getLaunchConfigurationType(handler.getTypeName());
39              ILaunchConfiguration[] configs = manager
40                      .getLaunchConfigurations(type);
41              for (ILaunchConfiguration element : configs) {
42                  if (element.getName().equals(handler.getConfigName())) {
43                      if (handler.equals(element)) {
44                          config = element;
45                      } else {
46                          element.delete();
47                      }
48                      break;
49                  }
50              }
51              if (config == null) {
52                  ILaunchConfigurationWorkingCopy copy = type.newInstance(null,
53                          handler.getConfigName());
54                  handler.setUp(copy);
55                  config = copy.doSave();
56              }
57          } catch (CoreException e) {
58              CommonPlugin.log(e);
59          }
60          return config;
61      }
62  
63      public interface CreationHandler {
64  
65          String getTypeName();
66  
67          String getConfigName();
68  
69          boolean equals(ILaunchConfiguration config);
70  
71          void setUp(ILaunchConfigurationWorkingCopy config);
72      }
73  }