source: trunk/www/admin/plugindefinitions/index.jsp @ 3712

Last change on this file since 3712 was 3712, checked in by Martin Svensson, 14 years ago

References #364. Most of the plugin settings can be saved now, but still some things left to add

  • Property svn:eol-style set to native
  • Property svn:keywords set to Date Id
File size: 18.1 KB
Line 
1<%-- $Id: index.jsp 3712 2007-09-06 12:56:57Z martin $
2  ------------------------------------------------------------------
3  Copyright (C) 2005 Nicklas Nordborg
4  Copyright (C) 2006 Jari Hakkinen, Nicklas Nordborg, Martin Svensson
5  Copyright (C) 2007 Nicklas Nordborg
6
7  This file is part of BASE - BioArray Software Environment.
8  Available at http://base.thep.lu.se/
9
10  BASE is free software; you can redistribute it and/or
11  modify it under the terms of the GNU General Public License
12  as published by the Free Software Foundation; either version 2
13  of the License, or (at your option) any later version.
14
15  BASE is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  GNU General Public License for more details.
19
20  You should have received a copy of the GNU General Public License
21  along with this program; if not, write to the Free Software
22  Foundation, Inc., 59 Temple Place - Suite 330,
23  Boston, MA  02111-1307, USA.
24  ------------------------------------------------------------------
25
26  @author Nicklas
27  @version 2.0
28--%>
29<%@ page session="false"
30  import="net.sf.basedb.core.Application"
31  import="net.sf.basedb.core.SessionControl"
32  import="net.sf.basedb.core.DbControl"
33  import="net.sf.basedb.core.Item"
34  import="net.sf.basedb.core.Include"
35  import="net.sf.basedb.core.PluginDefinition"
36  import="net.sf.basedb.core.JobAgent"
37  import="net.sf.basedb.core.JobAgentSettings"
38  import="net.sf.basedb.core.ItemQuery"
39  import="net.sf.basedb.core.ItemResultList"
40  import="net.sf.basedb.core.Permission"
41  import="net.sf.basedb.core.ItemContext"
42  import="net.sf.basedb.core.MultiPermissions"
43  import="net.sf.basedb.core.BaseException"
44  import="net.sf.basedb.core.PermissionDeniedException"
45  import="net.sf.basedb.core.ItemAlreadyExistsException"
46  import="net.sf.basedb.core.RoleKey"
47  import="net.sf.basedb.core.plugin.Plugin"
48  import="net.sf.basedb.core.query.Hql"
49  import="net.sf.basedb.util.FileUtil"
50  import="net.sf.basedb.util.PluginInfo"
51  import="net.sf.basedb.util.RemovableUtil"
52  import="net.sf.basedb.util.ShareableUtil"
53  import="net.sf.basedb.util.OwnableUtil"
54  import="net.sf.basedb.util.JarClassLoader"
55  import="net.sf.basedb.clients.web.Base"
56  import="net.sf.basedb.clients.web.PermissionUtil"
57  import="net.sf.basedb.clients.web.WebException"
58  import="net.sf.basedb.util.Values"
59  import="net.sf.basedb.clients.web.util.HTML"
60  import="java.util.Enumeration"
61  import="java.util.Set"
62  import="java.util.HashSet"
63  import="java.util.List"
64  import="java.util.ArrayList"
65  import="java.util.Collections"
66%>
67<%@ taglib prefix="base" uri="/WEB-INF/base.tld" %>
68<%!
69  private static final ItemContext defaultContext = Base.createDefaultContext("name", "name,pluginType,className,description");
70  private static final Item itemType = Item.PLUGINDEFINITION;
71%>
72<%
73final SessionControl sc = Base.getExistingSessionControl(pageContext, true);
74final String ID = sc.getId();
75final String cmd = request.getParameter("cmd");
76final String root = request.getContextPath()+"/";
77final String mode = request.getParameter("mode");
78final String callback = request.getParameter("callback");
79final String itemId = request.getParameter("item_id");
80final String listPage = "list_plugins.jsp?ID="+ID
81  +(mode == null ? "" : "&mode="+mode)
82  +(callback == null ? "" : "&callback="+callback)
83  +(itemId == null ? "" : "&item_id="+itemId);
84final String viewPage = "view_plugin.jsp?ID="+ID;
85final String editPage = "edit_plugin.jsp?ID="+ID;
86
87String forward = null;
88String redirect = null;
89String message = null;
90DbControl dc = null;
91
92try
93{
94  if (cmd == null || "List".equals(cmd))
95  {
96    // Display the list page without updatinging the current context
97    Base.getAndSetCurrentContext(sc, itemType, null, defaultContext, true);
98    redirect = listPage;
99  }
100  else if ("UpdateContext".equals(cmd))
101  {
102    // Display the list page after updating the current context from the request parameters
103    Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
104    redirect = listPage;
105  }
106  else if ("LoadContext".equals(cmd))
107  {
108    // Display the list page after loading a saved context
109    int contextId = Values.getInt(request.getParameter("context"));
110    Base.loadContext(sc, contextId, defaultContext);
111    redirect = listPage;
112  }
113
114  else if ("ViewItem".equals(cmd))
115  {
116    // Display the view page for a single item
117    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
118    forward = viewPage;
119  }
120  else if ("EditItem".equals(cmd))
121  {
122    // Display the edit page for a single item (should be opened in a popup)
123    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
124    forward = editPage;
125  }
126  else if ("NewItem".equals(cmd))
127  {
128    // Display the edit page for a new item (should be opened in a popup)
129    if (!sc.hasPermission(Permission.CREATE, itemType))
130    {
131      throw new PermissionDeniedException(Permission.CREATE, itemType.toString());
132    }
133    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
134    cc.setId(0);
135    if (Application.getNonCorePluginDirectory() == null)
136    {
137      redirect = editPage;
138    }
139    else
140    {
141      String pluginPath = Application.getNonCorePluginDirectory().getPath();
142      redirect = "select_installation_type.jsp?ID="+ID+"&plugin_path="+pluginPath;
143    }
144  }
145  else if("InstallationType".equals(cmd))
146  {
147    //Either manually or auto installation is selected
148    if (request.getParameter("installationtypes").equals("autoinstall"))
149    {
150      String pluginPath = request.getParameter("plugin_path");
151      redirect = "auto_install.jsp?ID="+ID+"&plugin_path="+pluginPath;
152    }
153    else if (request.getParameter("installationtypes").equals("manuellinstall"))
154    {
155      redirect = editPage;
156    }
157   
158  }
159  else if("LoadPlugins".equals(cmd))
160  {
161    //Loads plugins from jars that have been selected in auto_installer.jsp
162    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, null, defaultContext);
163    dc = sc.newDbControl();
164    java.io.File pluginDir = new java.io.File(request.getParameter("plugin_path"));
165    List<PluginInfo> pluginInfos = new ArrayList<PluginInfo>();
166    List<java.io.File> jarFiles = FileUtil.findFiles(pluginDir, null, "jar", true);
167   
168    for (java.io.File file : jarFiles)
169    {
170      pluginInfos.addAll(PluginInfo.loadFromJar(file));
171    }
172    for (PluginInfo info : pluginInfos)
173    {
174      String selectedOption = request.getParameter(info.getJarPath() + "." + info.getClassName());
175      if (selectedOption == null) selectedOption = "";
176      StringBuilder installedPlugins = new StringBuilder();
177      StringBuilder notInstalledPlugins = new StringBuilder();
178      boolean classExists = Values.getBoolean(request.getParameter(info.getClassName()+".classExists"));
179      int usePermissions = Values.getInt(request.getParameter("use_permissions"));
180      if (selectedOption.equals("plugin") || selectedOption.equals("plugin+configurations"))
181      {
182        if (!classExists)
183        {
184          PluginDefinition pd = null;
185          try
186          {
187            pd = PluginDefinition.getNew(dc, info.getClassName(), info.getJarPath(), usePermissions==2);
188          }
189          catch(BaseException bex)
190          {
191            notInstalledPlugins.append("Could notinstall: " + 
192                info.getClassName() + " in jar:" + info.getJarPath() + "\n");
193          }
194          if (pd != null)
195          {
196            installedPlugins.append("Installed :" + pd.getClassName() + "\n");
197            dc.saveItem(pd);
198            pd.setTrusted(Values.getBoolean(request.getParameter("trusted")));
199            String aie = Values.getStringOrNull(request.getParameter("allow_immediate_execution"));
200            pd.setAllowImmediateExecution(aie == null ? 
201              pd.getMainType() == Plugin.MainType.EXPORT : Values.getBoolean(aie));           
202          }
203        }
204      }
205    }
206    dc.commit();
207    cc.removeObject("item");
208  }
209  else if ("UpdateItem".equals(cmd))
210  {
211    // Update the properties on an item (will close the popup)
212    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, null, defaultContext);
213    dc = sc.newDbControl();
214    PluginDefinition plugin = (PluginDefinition)cc.getObject("item");
215    String className = Values.getStringOrNull(request.getParameter("className"));
216    String jarPath = Values.getStringOrNull(request.getParameter("jarPath"));
217    int usePermissions = Values.getInt(request.getParameter("use_permissions"));
218    if (plugin == null)
219    {
220      plugin = PluginDefinition.getNew(dc, className, jarPath, usePermissions == 2);
221      message = "Plugin created";
222      dc.saveItem(plugin);
223    }
224    else
225    {
226      dc.reattachItem(plugin);
227      plugin.loadPluginInformation(jarPath, className, usePermissions == 2);
228      message = "Plugin updated";
229    }
230    plugin.setMaxMemory(Values.parseBytes(request.getParameter("maxMemory"), null));
231    plugin.setTrusted(Values.getBoolean(request.getParameter("trusted")));
232    String aie = Values.getStringOrNull(request.getParameter("allow_immediate_execution"));
233    plugin.setAllowImmediateExecution(aie == null ? 
234      plugin.getMainType() == Plugin.MainType.EXPORT : Values.getBoolean(aie));
235
236    //Update permissions
237    ItemQuery<RoleKey> q = RoleKey.getQuery();
238    q.setCacheResult(true);
239    ItemResultList<RoleKey> roleKeys = q.list(dc);
240    for (RoleKey key : roleKeys)
241    {
242      String name = key.getItemType().name();
243      int granted = Values.getInt(request.getParameter(name+"_granted"));
244      int denied = Values.getInt(request.getParameter(name+"_denied"));
245      plugin.setPermissions(key, PermissionUtil.getPermissions(granted), PermissionUtil.getPermissions(denied));
246    }
247    plugin.setUsePermissions(usePermissions != 0);
248   
249    // Installed on job agents
250    String[] removeAgents = Values.getString(request.getParameter("removeAgents")).split(",");
251    for (int i = 0; i < removeAgents.length; ++i)
252    {
253      int agentId = Values.getInt(removeAgents[i], -1);
254      if (agentId != -1) 
255      {
256        JobAgent agent = JobAgent.getById(dc, agentId);
257        JobAgentSettings settings = agent.getSettings(plugin, false);
258        if (settings != null && settings.isInDatabase()) dc.deleteItem(settings);
259      }
260    }
261
262    String[] allAgents = Values.getString(request.getParameter("allAgents")).split(",");
263    for (int i = 0; i < allAgents.length; ++i)
264    {
265      int agentId = Values.getInt(allAgents[i], -1);
266      if (agentId != -1) 
267      {
268        JobAgent agent = JobAgent.getById(dc, agentId);
269        JobAgentSettings settings = agent.getSettings(plugin, true);
270        if (!settings.isInDatabase()) 
271        {
272          dc.saveItem(settings);
273        }
274        else
275        {
276          dc.reattachItem(settings);
277        }
278        settings.setJarPath(Values.getStringOrNull(request.getParameter("jarPath."+agentId)));
279        settings.setMaxMemory(Values.parseBytes(request.getParameter("maxMemory."+agentId), null));
280        settings.setPriorityBoost(Values.getInt(request.getParameter("priorityBoost."+agentId)));
281        String trusted = request.getParameter("trusted."+agentId);
282        settings.setTrusted(trusted == null ? null : Values.getBoolean(trusted));
283      }
284    }
285   
286    dc.commit();
287    cc.removeObject("item");
288  }
289  else if ("DeleteItem".equals(cmd))
290  {
291    // Delete a single item and then return to the view page
292    dc = sc.newDbControl();
293    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
294    RemovableUtil.setRemoved(dc, itemType, Collections.singleton(cc.getId()), true);
295    dc.commit();
296    redirect = viewPage;
297  }
298  else if ("DeleteItems".equals(cmd))
299  {
300    // Delete all selected items on the list page
301    dc = sc.newDbControl();
302    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
303    int numTotal = cc.getSelected().size();
304    int numRemoved = RemovableUtil.setRemoved(dc, itemType, cc.getSelected(), true);
305    dc.commit();
306    if (numTotal != numRemoved)
307    {
308      message = (numRemoved == 0 ? "No" : "Only "+numRemoved+" of "+numTotal) + " items could be deleted, because you have no DELETE permission";
309    }
310    redirect = listPage+(message != null ? "&popmessage="+HTML.urlEncode(message) : "");
311  }
312  else if ("RestoreItem".equals(cmd))
313  {
314    // Restore a single item and then return to the view page
315    dc = sc.newDbControl();
316    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
317    RemovableUtil.setRemoved(dc, itemType, Collections.singleton(cc.getId()), false);
318    dc.commit();
319    redirect = viewPage;
320  }
321  else if ("RestoreItems".equals(cmd))
322  {
323    // Restore all selected items on the list page
324    dc = sc.newDbControl();
325    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
326    int numTotal = cc.getSelected().size();
327    int numRemoved = RemovableUtil.setRemoved(dc, itemType, cc.getSelected(), false);
328    dc.commit();
329    if (numTotal != numRemoved)
330    {
331      message = (numRemoved == 0 ? "No" : "Only "+numRemoved+" of "+numTotal) + " items could be restored, because you have no WRITE permission";
332    }
333    redirect = listPage+(message != null ? "&popmessage="+HTML.urlEncode(message) : "");
334  }
335  else if ("ShareItem".equals(cmd))
336  {
337    // Display a popup window for sharing a single item
338    dc = sc.newDbControl();
339    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
340    MultiPermissions permissions = ShareableUtil.getMultiPermissions(dc, itemType, Collections.singleton(cc.getId()));
341    dc.close();
342    cc.setObject("MultiPermissions", permissions);
343    redirect = "../../common/share/share.jsp?ID="+ID+"&item_type="+itemType.name();
344  }
345  else if ("ShareItems".equals(cmd))
346  {
347    // Display a popup window for sharing all selected items on the list page
348    dc = sc.newDbControl();
349    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
350    MultiPermissions permissions = ShareableUtil.getMultiPermissions(dc, itemType, cc.getSelected());
351    dc.close();
352    cc.setObject("MultiPermissions", permissions);
353    redirect = "../../common/share/share.jsp?ID="+ID+"&item_type="+itemType.name();
354  }
355  else if ("TakeOwnershipOfItem".equals(cmd))
356  {
357    // Take ownership a single item and then return to the view page
358    dc = sc.newDbControl();
359    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
360    OwnableUtil.setOwner(dc, itemType, Collections.singleton(cc.getId()), null);
361    dc.commit();
362    redirect = viewPage;
363  }
364  else if ("TakeOwnershipOfItems".equals(cmd))
365  {
366    // Take ownership all selected items on the list page
367    dc = sc.newDbControl();
368    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
369    int numTotal = cc.getSelected().size();
370    int numOwned = OwnableUtil.setOwner(dc, itemType, cc.getSelected(), null);
371    dc.commit();
372    if (numTotal != numOwned)
373    {
374      message = (numOwned == 0 ? "No" : "Only "+numOwned+" of "+numTotal) + " items could be taken, because you have no SET_OWNER permission";
375    }
376    redirect = listPage+(message != null ? "&popmessage="+HTML.urlEncode(message) : "");
377  }
378  else if ("ExportItems".equals(cmd))
379  {
380    // Run an export plugin in a list context
381    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
382    final ItemQuery<PluginDefinition> query = PluginDefinition.getQuery();
383    query.join(Hql.leftJoin("guiContexts", "ctx"));
384    query.setDistinct(true);
385    cc.configureQuery(query, true);
386    cc.setQuery(query);
387    redirect = "../../common/export/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=LIST&title=Export+plugins";
388  }
389  else if ("ExportItem".equals(cmd))
390  {
391    // Run an export plugin in single-item context
392    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
393    redirect = "../../common/export/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=ITEM&title=Export+plugin";
394  }
395  else if ("ImportItems".equals(cmd))
396  {
397    // Run an import plugin in a list context
398    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
399    final ItemQuery<PluginDefinition> query = PluginDefinition.getQuery();
400    query.join(Hql.leftJoin("guiContexts", "ctx"));
401    query.setDistinct(true);
402    cc.configureQuery(query, true);
403    cc.setQuery(query);
404    redirect = "../../common/import/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=LIST&title=Import+plugins";
405  }
406  else if ("ImportItem".equals(cmd))
407  {
408    // Run an import plugin in single-item context
409    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
410    redirect = "../../common/import/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=ITEM&title=Import+plugin";
411  }
412  else if ("RunListPlugin".equals(cmd))
413  {
414    // Run another plugin in a list context
415    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
416    final ItemQuery<PluginDefinition> query = PluginDefinition.getQuery();
417    query.join(Hql.leftJoin("guiContexts", "ctx"));
418    query.setDistinct(true);
419    cc.configureQuery(query, true);
420    cc.setQuery(query);
421    redirect = "../../common/plugin/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=LIST&main_type=OTHER&title=Run+plugin";
422  }
423  else if ("RunPlugin".equals(cmd))
424  {
425    // Run another plugin in single-item context
426    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
427    redirect = "../../common/plugin/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=ITEM&main_type=OTHER&title=Run+plugin";
428  }
429  else if ("UnloadItems".equals(cmd))
430  {
431    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
432    dc = sc.newDbControl();
433    for (int id : cc.getSelected())
434    {
435      PluginDefinition pd = PluginDefinition.getById(dc, id);
436      if (pd.getJarPath() != null)
437      {
438        JarClassLoader.unload(pd.getJarPath());
439      }
440    }
441    dc.close();
442    redirect = listPage;
443  }
444  else if ("UnloadItem".equals(cmd))
445  {
446    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
447    dc = sc.newDbControl();
448    PluginDefinition pd = PluginDefinition.getById(dc, cc.getId());
449    if (pd.getJarPath() != null)
450    {
451      JarClassLoader.unload(pd.getJarPath());
452    }
453    dc.close();
454    redirect = viewPage;
455  }
456  else
457  {
458    throw new WebException("popup", "Invalid command", "The command {1} is not recognised as a valid command.", cmd);
459  }
460}
461finally
462{
463  if (dc != null) dc.close();
464}
465
466if (forward != null)
467{
468  pageContext.forward(forward);
469}
470else if (redirect != null)
471{
472  response.sendRedirect(redirect);
473}
474else if (message == null)
475{
476  response.sendRedirect(root + "common/close_popup.jsp?refresh_opener=1&wait=0");
477}
478else
479{
480  response.sendRedirect(root + "common/close_popup.jsp?refresh_opener=1&message="+HTML.urlEncode(message));
481}
482%>
483
Note: See TracBrowser for help on using the repository browser.