Wiki source code of MongoDB

Last modified by Pierre Dubois on 2015/06/11 11:52

Show last authors
1 = Use MongoDB =
2
3 If your application uses an important set of values, you can choose to store them in MongoDB :
4
5 * create a database: use databaseName;
6 * create a user on it: db.createUser({user : "username" ,pwd : "password",roles :["readWrite"]}) ;
7
8 In the System Parameters, modify MongoHost, MongoPort, MongoDatabase, MongoUser and MongoPassword
9 [[image:Capturedecran2015-06-04a15.45.54.png]]
10
11 You have now to create a java class in order to make your bundle to listen to the service **TimeSerieMongoClientService**. In the pom file’s directory of your bundle execute following command:
12
13 * mvm clean install
14 * mvn eclipse:eclipse
15
16 Then import the bundle as an eclipse project to add that class which activate the service :
17 Right click -> Import -> General -> existing project into workspace and specify the folder of your bundle in tomcat/dysoweb.home/application
18 [[image:Capturedecran2015-06-04a16.13.56.png]]
19
20 Create a class Activator in the folder src/main/java:
21
22 {{code language="java"}}
23 public class Activator implements BundleActivator {
24
25 private BundleContext fContext;
26 private IApplicationService fApplicationService;
27 private static Activator fInstance;
28 private ITimeSeriesMongoClientService fMongoService;
29
30 public Activator() {
31
32 }
33
34 public void start(BundleContext context) throws Exception {
35 fContext = context;
36 registerApplicationServiceListener(context);
37 registerMongoServiceListener(context);
38 fInstance = this;
39 }
40
41 public void stop(BundleContext context) throws Exception {
42 fContext = null;
43 fInstance = null;
44 }
45
46 public static Activator getDefault() {
47 return fInstance;
48 }
49
50 public IApplicationService getApplicationService() {
51 // retrieve the import service for the running instance
52 return fApplicationService;
53 }
54
55 private void registerApplicationServiceListener(BundleContext context) throws InvalidSyntaxException {
56 // get the service ref if the auth plugin is already started
57 ServiceReference sr = context.getServiceReference(IApplicationService.class.getName());
58 if(sr != null) {
59 fApplicationService = (IApplicationService)context.getService(sr);
60 }
61
62 ServiceListener sl = new ServiceListener() {
63
64 @Override
65 public void serviceChanged(ServiceEvent ev) {
66 ServiceReference sr = ev.getServiceReference();
67 switch (ev.getType()) {
68 case ServiceEvent.REGISTERED: {
69 fApplicationService = (IApplicationService)fContext.getService(sr);
70 }
71 break;
72 case ServiceEvent.UNREGISTERING: {
73 fApplicationService = null;
74 }
75 break;
76 }
77 }
78 };
79
80 String filter = "(objectclass="
81 + IApplicationService.class.getName() + ")";
82 context.addServiceListener(sl, filter);
83 }
84
85 private void registerMongoServiceListener(BundleContext context) throws InvalidSyntaxException {
86 // get the service ref if the auth plugin is already started
87 ServiceReference sr = context.getServiceReference(ITimeSeriesMongoClientService.class.getName());
88 if(sr != null) {
89 fMongoService = (ITimeSeriesMongoClientService)context.getService(sr);
90 }
91
92 ServiceListener sl = new ServiceListener() {
93
94 @Override
95 public void serviceChanged(ServiceEvent ev) {
96 ServiceReference sr = ev.getServiceReference();
97 switch (ev.getType()) {
98 case ServiceEvent.REGISTERED: {
99 fMongoService = (ITimeSeriesMongoClientService)fContext.getService(sr);
100 }
101 break;
102 case ServiceEvent.UNREGISTERING: {
103 fMongoService = null;
104 }
105 break;
106 }
107 }
108 };
109
110 String filter = "(objectclass="
111 + ITimeSeriesMongoClientService.class.getName() + ")";
112 context.addServiceListener(sl, filter);
113 }
114
115 public ITimeSeriesMongoClientService getMongoService() {
116 return this.fMongoService;
117 }
118 }
119
120 {{/code}}
121
122 To solve import errors, edit the pom.xml file:
123
124 * add a dependency:
125
126 {{code language="xml"}}
127 <dependency>
128 <groupId>com.requea.dynapage</groupId>
129 <artifactId>com.requea.dynapage.mongo</artifactId>
130 <scope>provided</scope>
131 <version>4.0.245</version>
132 </dependency>
133
134 {{/code}}
135
136 * add an import package:
137
138 com.requea.dynapage.mongo,
139
140 * add a field bundle-activator after Import-Package mark:
141
142 {{code language="xml"}}
143 <Bundle-Activator>com.requea.schneider.dlvm.Activator</Bundle-Activator>
144 {{/code}}
145
146
147 Under src/main/META-INF, edit MANIFEST.MF:
148
149 * add com.requea.dynapage.mongo in the section Import-Package
150 * add the section Bundle-Activator: com.requea.name.Activator
151
152 Recompile the bundle with the command line mvm eclipse:eclipse
153
154 You can now add your bundle by typing that command in the shell platform (under http://localhost:8080/dysoweb/dysoweb/panel/secure/shell.jsp):
155
156 start reference:file: /path to the bundle //com.requea.name///src/main
157
158 If you change the version of Mongo and get an authentication error, you have to delete and recreate the folder /data/db
This wiki is licensed under a Creative Commons 2.0 license
XWiki Enterprise 9.11.5 - Documentation