source: trunk/autoquest-ui-core/src/main/java/de/ugoe/cs/autoquest/commands/misc/CMDload.java

Last change on this file was 2285, checked in by pharms, 5 years ago

solved class loader issues

  • Property svn:mime-type set to text/plain
File size: 3.6 KB
Line 
1//   Copyright 2012 Georg-August-Universität Göttingen, Germany
2//
3//   Licensed under the Apache License, Version 2.0 (the "License");
4//   you may not use this file except in compliance with the License.
5//   You may obtain a copy of the License at
6//
7//       http://www.apache.org/licenses/LICENSE-2.0
8//
9//   Unless required by applicable law or agreed to in writing, software
10//   distributed under the License is distributed on an "AS IS" BASIS,
11//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12//   See the License for the specific language governing permissions and
13//   limitations under the License.
14
15package de.ugoe.cs.autoquest.commands.misc;
16
17import java.io.File;
18import java.io.FileInputStream;
19import java.io.IOException;
20import java.io.ObjectInputStream;
21import java.io.ObjectStreamClass;
22import java.util.List;
23
24import de.ugoe.cs.autoquest.plugin.AutoQUESTPlugin;
25import de.ugoe.cs.autoquest.plugin.PluginLoader;
26import de.ugoe.cs.util.console.Command;
27import de.ugoe.cs.util.console.CommandExecuter;
28import de.ugoe.cs.util.console.Console;
29import de.ugoe.cs.util.console.GlobalDataContainer;
30
31/**
32 * <p>
33 * Command that loads a previously serialized {@link GlobalDataContainer}.
34 * </p>
35 *
36 * @author Steffen Herbold
37 * @version 1.0
38 */
39public class CMDload implements Command {
40
41        /*
42         * (non-Javadoc)
43         *
44         * @see de.ugoe.cs.util.console.Command#run(java.util.List)
45         */
46        @Override
47        public void run(List<Object> parameters) {
48                String filename;
49                try {
50                        filename = (String) parameters.get(0);
51                } catch (Exception e) {
52                        throw new IllegalArgumentException();
53                }
54
55                final PluginLoader pluginLoader = new PluginLoader(new File("lib"));
56                pluginLoader.load();
57
58                FileInputStream fis = null;
59                ObjectInputStream in = null;
60                try {
61                        fis = new FileInputStream(filename);
62                        in = new ObjectInputStream(fis) {
63                            @Override
64                            protected Class<?> resolveClass(ObjectStreamClass clazz)
65                                    throws IOException, ClassNotFoundException
66                            {
67                                try {
68                                    return this.getClass().getClassLoader().loadClass(clazz.getName());
69                                }
70                                catch (Exception e) {
71                                    for (AutoQUESTPlugin plugin : pluginLoader.getPlugins()) {
72                                        try {
73                                            return plugin.getClass().getClassLoader().loadClass(clazz.getName());
74                                        }
75                                        catch (Exception e2) {
76                                            // ignore and try next
77                                        }
78                                    }
79                                }
80                               
81                                throw new ClassNotFoundException("could not load class " + clazz.getName() +
82                                                                 " neither from normal class loader nor from " +
83                                                                 "plug in class loaders");
84                            }
85                        };
86                        in.readObject();
87                        in.close();
88                } catch (IOException ex) {
89                        Console.logException(ex);
90                } catch (ClassNotFoundException ex) {
91                        Console.logException(ex);
92                }
93        }
94
95        /*
96         * (non-Javadoc)
97         *
98         * @see de.ugoe.cs.util.console.Command#help()
99         */
100        @Override
101        public String help() {
102                return "load <filename>";
103        }
104
105}
Note: See TracBrowser for help on using the repository browser.