Skip to main content
summaryrefslogtreecommitdiffstats
blob: 6fda65f9c1deb83847fa86f99a19144087acfff0 (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
/*******************************************************************************
 * Copyright (c) 2004, 2007 Boeing.
 * 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:
 *     Boeing - initial API and implementation
 *******************************************************************************/
package org.eclipse.osee.framework.server.ide;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Collections;
import java.util.Dictionary;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Future;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.osee.framework.core.server.UnsecuredOseeHttpServlet;
import org.eclipse.osee.framework.core.util.OsgiUtil;
import org.eclipse.osee.framework.jdk.core.util.Lib;
import org.eclipse.osee.framework.jdk.core.util.Strings;
import org.eclipse.osee.logger.Log;
import org.eclipse.osgi.framework.console.CommandInterpreter;
import org.eclipse.osgi.framework.console.CommandProvider;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.util.tracker.ServiceTracker;

/**
 * @author Roberto E. Escobar
 */
public class AdminServlet extends UnsecuredOseeHttpServlet {

   private static final long serialVersionUID = -4391079960307521104L;

   private final BundleContext context;

   public AdminServlet(Log logger, BundleContext context) {
      super(logger);
      this.context = context;
   }

   @Override
   protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
      resp.setStatus(HttpServletResponse.SC_OK);
      PrintWriter writer = resp.getWriter();

      Map<String, CommandProvider> cmds = getCommands(context);
      for (CommandProvider commandProvider : cmds.values()) {
         writer.append(commandProvider.getHelp());
      }
   }

   @Override
   protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
      String cmd = req.getParameter("cmd");
      String argList = req.getParameter("args");

      List<String> args;
      if (Strings.isValid(argList)) {
         args = Arrays.asList(argList.split(","));
      } else {
         args = Collections.emptyList();
      }
      resp.setStatus(HttpServletResponse.SC_OK);
      CommandInterpreter interpreter = new HttpCommandInterpreter(context, resp.getWriter(), args.iterator());
      Object object = interpreter.execute(cmd);
      if (object instanceof Job) {
         Job job = (Job) object;
         try {
            job.join();
            IStatus status = job.getResult();
            interpreter.println(status.toString());
         } catch (InterruptedException ex) {
            interpreter.print(ex);
         }
      } else if (object instanceof Future<?>) {
         Future<?> future = (Future<?>) object;
         try {
            future.get();
         } catch (Exception ex) {
            interpreter.print(ex);
         }
      }

   }

   private static String commandKey(String rawCommand) {
      return "_" + rawCommand;
   }

   private static Map<String, CommandProvider> getCommands(BundleContext context) {
      Map<String, CommandProvider> data = new HashMap<>();
      ServiceTracker<CommandProvider, CommandProvider> tracker =
         new ServiceTracker<CommandProvider, CommandProvider>(context, CommandProvider.class, null);
      tracker.open(true);
      try {
         Object[] services = tracker.getServices();
         for (Object service : services) {
            CommandProvider commandProvider = (CommandProvider) service;
            for (Method method : commandProvider.getClass().getMethods()) {
               String methodName = method.getName();
               if (methodName.startsWith("_")) {
                  data.put(methodName, commandProvider);
               }
            }
         }
      } finally {
         OsgiUtil.close(tracker);
      }
      return data;
   }

   private static final class HttpCommandInterpreter implements CommandInterpreter {

      private final Writer writer;
      private final BundleContext context;
      private final Iterator<String> args;

      public HttpCommandInterpreter(BundleContext context, Writer writer, Iterator<String> args) {
         this.writer = writer;
         this.context = context;
         this.args = args;
      }

      @Override
      public String nextArgument() {
         return args.hasNext() ? args.next() : null;
      }

      @Override
      public Object execute(String cmd) {
         String methodName = commandKey(cmd);
         Map<String, CommandProvider> commands = getCommands(context);
         CommandProvider commandProvider = commands.get(methodName);
         Class<?> providerClass = commandProvider.getClass();

         Object toReturn = null;
         try {
            Method method = providerClass.getMethod(methodName, CommandInterpreter.class);
            toReturn = method.invoke(commandProvider, this);
         } catch (Exception ex) {
            print(ex);
         }
         return toReturn;
      }

      @Override
      public void print(Object o) {
         print(String.valueOf(o));
      }

      @Override
      public void println() {
         print("\n");
      }

      @Override
      public void println(Object o) {
         print(String.format("%s\n", o));
      }

      @Override
      public void printStackTrace(Throwable t) {
         print(Lib.exceptionToString(t));
      }

      @SuppressWarnings("rawtypes")
      @Override
      public void printDictionary(Dictionary dic, String title) {
         print(String.format("%s:[%s]", title, dic));
      }

      @Override
      public void printBundleResource(Bundle bundle, String resource) {
         print(String.format("[%s:%s]", bundle, resource));
      }

      private void print(String message) {
         try {
            writer.append(message);
         } catch (Exception ex) {
            // Do Nothing
         }
      }
   }
}

Back to the top