1 package rydeen;
2
3 import java.io.IOException;
4 import java.util.Iterator;
5
6 import rydeen.spi.ProcessorService;
7 import rydeen.spi.ProcessorServiceLoader;
8 import rydeen.spi.ProcessorServiceLoaderFactory;
9
10
11
12
13
14
15
16
17 public class Environment implements Iterable<ProcessorService>{
18 private ProcessorServicePool pool;
19
20
21
22
23 public Environment(ClassLoader loader){
24 initialize(loader);
25 }
26
27
28
29
30
31
32 public Environment(){
33 initialize(buildClassLoader());
34 }
35
36
37
38
39
40 public Iterator<ProcessorService> iterator(){
41 return pool.iterator();
42 }
43
44
45
46
47
48
49 public ProcessorService getService(String name){
50 if(name == null){
51 throw new NullPointerException();
52 }
53 return pool.getService(name);
54 }
55
56
57
58
59
60 public void updateClassLoader(ClassLoader loader){
61 ProcessorServiceLoader serviceLoader = ProcessorServiceLoaderFactory.getInstance().getLoader();
62 serviceLoader.loadServices(pool, loader);
63 }
64
65
66
67
68
69 public int getServiceCount(){
70 return pool.getServiceCount();
71 }
72
73 private ClassLoader buildClassLoader(){
74 ClassLoaderBuilder builder = new LocalClassLoaderBuilder();
75 ClassLoader loader = null;
76 try{
77 loader = builder.createLoader();
78 } catch(IOException e){
79 e.printStackTrace();
80 }
81 return loader;
82 }
83
84 private void initialize(ClassLoader loader){
85 pool = new ProcessorServicePool();
86
87 updateClassLoader(loader);
88 }
89 }