KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > descriptor > DescriptorServiceImpl


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.repo.descriptor;
18
19 import java.io.IOException JavaDoc;
20 import java.io.Serializable JavaDoc;
21 import java.lang.reflect.Constructor JavaDoc;
22 import java.lang.reflect.InvocationTargetException JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.Map JavaDoc;
25 import java.util.Properties JavaDoc;
26
27 import org.alfresco.error.AlfrescoRuntimeException;
28 import org.alfresco.model.ContentModel;
29 import org.alfresco.repo.importer.ImporterBootstrap;
30 import org.alfresco.repo.transaction.TransactionUtil;
31 import org.alfresco.repo.transaction.TransactionUtil.TransactionWork;
32 import org.alfresco.service.cmr.repository.NodeRef;
33 import org.alfresco.service.cmr.repository.NodeService;
34 import org.alfresco.service.cmr.repository.StoreRef;
35 import org.alfresco.service.cmr.search.SearchService;
36 import org.alfresco.service.descriptor.Descriptor;
37 import org.alfresco.service.descriptor.DescriptorService;
38 import org.alfresco.service.license.LicenseDescriptor;
39 import org.alfresco.service.license.LicenseException;
40 import org.alfresco.service.license.LicenseService;
41 import org.alfresco.service.namespace.NamespaceService;
42 import org.alfresco.service.namespace.QName;
43 import org.alfresco.service.transaction.TransactionService;
44 import org.apache.commons.logging.Log;
45 import org.apache.commons.logging.LogFactory;
46 import org.springframework.beans.BeansException;
47 import org.springframework.beans.factory.InitializingBean;
48 import org.springframework.context.ApplicationContext;
49 import org.springframework.context.ApplicationContextAware;
50 import org.springframework.context.ApplicationEvent;
51 import org.springframework.context.ApplicationListener;
52 import org.springframework.context.event.ContextRefreshedEvent;
53 import org.springframework.core.io.Resource;
54
55
56 /**
57  * Implementation of Descriptor Service
58  *
59  * @author David Caruana
60  */

61 public class DescriptorServiceImpl implements DescriptorService, ApplicationListener, InitializingBean, ApplicationContextAware
62 {
63     private static Log logger = LogFactory.getLog(DescriptorServiceImpl.class);
64     
65     private ApplicationContext applicationContext;
66     
67     private Properties JavaDoc serverProperties;
68     
69     private ImporterBootstrap systemBootstrap;
70     private NamespaceService namespaceService;
71     private NodeService nodeService;
72     private SearchService searchService;
73     private TransactionService transactionService;
74     private LicenseService licenseService = null;
75
76     private Descriptor serverDescriptor;
77     private Descriptor installedRepoDescriptor;
78
79     
80     /* (non-Javadoc)
81      * @see org.springframework.context.ApplicationContextAware#setApplicationContext(org.springframework.context.ApplicationContext)
82      */

83     public void setApplicationContext(ApplicationContext applicationContext) throws BeansException
84     {
85         this.applicationContext = applicationContext;
86     }
87     
88     /**
89      * Sets the server descriptor from a resource file
90      *
91      * @param descriptorResource resource containing server descriptor meta-data
92      * @throws IOException
93      */

94     public void setDescriptor(Resource descriptorResource)
95         throws IOException JavaDoc
96     {
97         this.serverProperties = new Properties JavaDoc();
98         this.serverProperties.load(descriptorResource.getInputStream());
99     }
100
101     /**
102      * @param systemBootstrap system bootstrap
103      */

104     public void setSystemBootstrap(ImporterBootstrap systemBootstrap)
105     {
106         this.systemBootstrap = systemBootstrap;
107     }
108     
109     /**
110      * @param transactionService transaction service
111      */

112     public void setTransactionService(TransactionService transactionService)
113     {
114         this.transactionService = transactionService;
115     }
116     
117     /**
118      * @param namespaceService namespace service
119      */

120     public void setNamespaceService(NamespaceService namespaceService)
121     {
122         this.namespaceService = namespaceService;
123     }
124
125     /**
126      * @param nodeService node service
127      */

128     public void setNodeService(NodeService nodeService)
129     {
130         this.nodeService = nodeService;
131     }
132     
133     /**
134      * @param searchService search service
135      */

136     public void setSearchService(SearchService searchService)
137     {
138         this.searchService = searchService;
139     }
140     
141     /* (non-Javadoc)
142      * @see org.alfresco.service.descriptor.DescriptorService#getDescriptor()
143      */

144     public Descriptor getServerDescriptor()
145     {
146         return serverDescriptor;
147     }
148
149     /* (non-Javadoc)
150      * @see org.alfresco.service.descriptor.DescriptorService#getRepositoryDescriptor()
151      */

152     public Descriptor getInstalledRepositoryDescriptor()
153     {
154         return installedRepoDescriptor;
155     }
156
157     /* (non-Javadoc)
158      * @see org.alfresco.service.descriptor.DescriptorService#getLicenseDescriptor()
159      */

160     public LicenseDescriptor getLicenseDescriptor()
161     {
162         return (licenseService == null) ? null : licenseService.getLicense();
163     }
164
165     /**
166      * @param event
167      */

168     public void onApplicationEvent(ApplicationEvent event)
169     {
170         if (event instanceof ContextRefreshedEvent)
171         {
172             // initialise the repository descriptor
173
// note: this requires that the repository schema has already been initialised
174
TransactionWork<Descriptor> createDescriptorWork = new TransactionUtil.TransactionWork<Descriptor>()
175             {
176                 public Descriptor doWork()
177                 {
178                     // initialise license service (if installed)
179
initialiseLicenseService();
180                     
181                     // verify license, but only if license component is installed
182
licenseService.verifyLicense();
183                     
184                     // persist the server descriptor values
185
updateCurrentRepositoryDescriptor(serverDescriptor);
186
187                     // return the repository installed descriptor
188
return createInstalledRepositoryDescriptor();
189                 }
190             };
191             installedRepoDescriptor = TransactionUtil.executeInUserTransaction(transactionService, createDescriptorWork);
192         }
193     }
194
195     /**
196      * Initialise Descriptors
197      */

198     public void afterPropertiesSet() throws Exception JavaDoc
199     {
200         // initialise server descriptor
201
serverDescriptor = createServerDescriptor();
202     }
203
204     /**
205      * Create server descriptor
206      *
207      * @return descriptor
208      */

209     private Descriptor createServerDescriptor()
210     {
211         return new ServerDescriptor();
212     }
213     
214     /**
215      * Create repository descriptor
216      *
217      * @return descriptor
218      */

219     private Descriptor createInstalledRepositoryDescriptor()
220     {
221         // retrieve system descriptor location
222
StoreRef storeRef = systemBootstrap.getStoreRef();
223         Properties JavaDoc systemProperties = systemBootstrap.getConfiguration();
224         String JavaDoc path = systemProperties.getProperty("system.descriptor.childname");
225
226         // retrieve system descriptor
227
NodeRef descriptorNodeRef = getDescriptorNodeRef(storeRef, path, false);
228         // create appropriate descriptor
229
if (descriptorNodeRef != null)
230         {
231             Map JavaDoc<QName, Serializable JavaDoc> properties = nodeService.getProperties(descriptorNodeRef);
232             return new RepositoryDescriptor(properties);
233         }
234         else
235         {
236             // descriptor cannot be found
237
return new UnknownDescriptor();
238         }
239     }
240     
241     /**
242      * Push the current server descriptor properties into persistence.
243      *
244      * @param serverDescriptor the current server descriptor
245      */

246     private void updateCurrentRepositoryDescriptor(Descriptor serverDescriptor)
247     {
248         // retrieve system descriptor location
249
StoreRef storeRef = systemBootstrap.getStoreRef();
250         Properties JavaDoc systemProperties = systemBootstrap.getConfiguration();
251         String JavaDoc path = systemProperties.getProperty("system.descriptor.current.childname");
252
253         // retrieve system descriptor
254
NodeRef currentDescriptorNodeRef = getDescriptorNodeRef(storeRef, path, true);
255         // if the node is missing but it should have been created
256
if (currentDescriptorNodeRef == null)
257         {
258             return;
259         }
260         // set the properties
261
nodeService.setProperty(currentDescriptorNodeRef, ContentModel.PROP_SYS_VERSION_MAJOR, serverDescriptor.getVersionMajor());
262         nodeService.setProperty(currentDescriptorNodeRef, ContentModel.PROP_SYS_VERSION_MINOR, serverDescriptor.getVersionMinor());
263         nodeService.setProperty(currentDescriptorNodeRef, ContentModel.PROP_SYS_VERSION_REVISION, serverDescriptor.getVersionRevision());
264         nodeService.setProperty(currentDescriptorNodeRef, ContentModel.PROP_SYS_VERSION_LABEL, serverDescriptor.getVersionLabel());
265         nodeService.setProperty(currentDescriptorNodeRef, ContentModel.PROP_SYS_VERSION_SCHEMA, serverDescriptor.getSchema());
266         
267         // done
268
if (logger.isDebugEnabled())
269         {
270             logger.debug("Updated current repository descriptor properties: \n" +
271                     " node: " + currentDescriptorNodeRef + "\n" +
272                     " descriptor: " + serverDescriptor);
273         }
274     }
275     
276     /**
277      *
278      * @param storeRef the store to search
279      * @param path the path below the root node to search
280      * @param create true if the node must be created if missing. No properties will be set.
281      * @return Returns the node for the path, or null
282      */

283     private NodeRef getDescriptorNodeRef(StoreRef storeRef, String JavaDoc path, boolean create)
284     {
285         NodeRef descriptorNodeRef = null;
286         String JavaDoc searchPath = "/" + path;
287
288         // check for the store
289
if (nodeService.exists(storeRef))
290         {
291             NodeRef rootNodeRef = nodeService.getRootNode(storeRef);
292             List JavaDoc<NodeRef> nodeRefs = searchService.selectNodes(rootNodeRef, searchPath, null, namespaceService, false);
293             if (nodeRefs.size() == 1)
294             {
295                 descriptorNodeRef = nodeRefs.get(0);
296             }
297             else if (nodeRefs.size() == 0)
298             {
299             }
300             else if (nodeRefs.size() > 1)
301             {
302                 if (logger.isDebugEnabled())
303                 {
304                     logger.debug("Multiple descriptors: \n" +
305                             " store: " + storeRef + "\n" +
306                             " path: " + searchPath);
307                 }
308                 // get the first one
309
descriptorNodeRef = nodeRefs.get(0);
310             }
311         }
312         
313         if (descriptorNodeRef == null)
314         {
315             if (logger.isDebugEnabled())
316             {
317                 logger.debug("Descriptor not found: \n" +
318                         " store: " + storeRef + "\n" +
319                         " path: " + searchPath);
320             }
321
322             // create if necessary
323
if (create)
324             {
325                 storeRef = nodeService.createStore(storeRef.getProtocol(), storeRef.getIdentifier());
326                 NodeRef rootNodeRef = nodeService.getRootNode(storeRef);
327                 descriptorNodeRef = nodeService.createNode(
328                         rootNodeRef,
329                         ContentModel.ASSOC_CHILDREN,
330                         QName.createQName(path, namespaceService),
331                         QName.createQName("sys:descriptor", namespaceService)).getChildRef();
332                 if (logger.isDebugEnabled())
333                 {
334                     logger.debug("Created missing descriptor node: " + descriptorNodeRef);
335                 }
336             }
337         }
338         return descriptorNodeRef;
339     }
340     
341     /**
342      * Initialise License Service
343      */

344     private void initialiseLicenseService()
345     {
346         try
347         {
348             // NOTE: We could tie in the License Component via Spring configuration, but then it could
349
// be declaratively taken out in an installed environment.
350
Class JavaDoc licenseComponentClass = Class.forName("org.alfresco.license.LicenseComponent");
351             Constructor JavaDoc constructor = licenseComponentClass.getConstructor(new Class JavaDoc[] { ApplicationContext.class} );
352             licenseService = (LicenseService)constructor.newInstance(new Object JavaDoc[] { applicationContext });
353         }
354         catch (ClassNotFoundException JavaDoc e)
355         {
356             licenseService = new NOOPLicenseService();
357         }
358         catch (SecurityException JavaDoc e)
359         {
360             throw new AlfrescoRuntimeException("Failed to initialise license service", e);
361         }
362         catch (IllegalArgumentException JavaDoc e)
363         {
364             throw new AlfrescoRuntimeException("Failed to initialise license service", e);
365         }
366         catch (NoSuchMethodException JavaDoc e)
367         {
368             throw new AlfrescoRuntimeException("Failed to initialise license service", e);
369         }
370         catch (InvocationTargetException JavaDoc e)
371         {
372             throw new AlfrescoRuntimeException("Failed to initialise license service", e);
373         }
374         catch (InstantiationException JavaDoc e)
375         {
376             throw new AlfrescoRuntimeException("Failed to initialise license service", e);
377         }
378         catch (IllegalAccessException JavaDoc e)
379         {
380             throw new AlfrescoRuntimeException("Failed to initialise license service", e);
381         }
382     }
383
384     /**
385      * Dummy License Service
386      */

387     private class NOOPLicenseService implements LicenseService
388     {
389         /* (non-Javadoc)
390          * @see org.alfresco.service.license.LicenseService#verify()
391          */

392         public void verifyLicense() throws LicenseException
393         {
394         }
395
396         /* (non-Javadoc)
397          * @see org.alfresco.service.license.LicenseService#getLicense()
398          */

399         public LicenseDescriptor getLicense() throws LicenseException
400         {
401             return null;
402         }
403     }
404     
405     /**
406      * Unknown descriptor
407      *
408      * @author David Caruana
409      */

410     private class UnknownDescriptor implements Descriptor
411     {
412         /* (non-Javadoc)
413          * @see org.alfresco.service.descriptor.Descriptor#getVersionMajor()
414          */

415         public String JavaDoc getVersionMajor()
416         {
417             return "Unknown";
418         }
419
420         /* (non-Javadoc)
421          * @see org.alfresco.service.descriptor.Descriptor#getVersionMinor()
422          */

423         public String JavaDoc getVersionMinor()
424         {
425             return "Unknown";
426         }
427
428         /* (non-Javadoc)
429          * @see org.alfresco.service.descriptor.Descriptor#getVersionRevision()
430          */

431         public String JavaDoc getVersionRevision()
432         {
433             return "Unknown";
434         }
435
436         /* (non-Javadoc)
437          * @see org.alfresco.service.descriptor.Descriptor#getVersionLabel()
438          */

439         public String JavaDoc getVersionLabel()
440         {
441             return "Unknown";
442         }
443
444         /* (non-Javadoc)
445          * @see org.alfresco.service.descriptor.Descriptor#getVersion()
446          */

447         public String JavaDoc getVersion()
448         {
449             return "Unknown (pre 1.0.0 RC2)";
450         }
451
452         /* (non-Javadoc)
453          * @see org.alfresco.service.descriptor.Descriptor#getEdition()
454          */

455         public String JavaDoc getEdition()
456         {
457             return "Unknown";
458         }
459
460         /*
461          * (non-Javadoc)
462          * @see org.alfresco.service.descriptor.Descriptor#getSchema()
463          */

464         public int getSchema()
465         {
466             return 0;
467         }
468
469         /* (non-Javadoc)
470          * @see org.alfresco.service.descriptor.Descriptor#getDescriptorKeys()
471          */

472         public String JavaDoc[] getDescriptorKeys()
473         {
474             return new String JavaDoc[0];
475         }
476
477         /* (non-Javadoc)
478          * @see org.alfresco.service.descriptor.Descriptor#getDescriptor(java.lang.String)
479          */

480         public String JavaDoc getDescriptor(String JavaDoc key)
481         {
482             return null;
483         }
484     }
485     
486     /**
487      * Repository Descriptor whose meta-data is retrieved from the repository store
488      */

489     private class RepositoryDescriptor implements Descriptor
490     {
491         private Map JavaDoc<QName, Serializable JavaDoc> properties;
492         
493         
494         /**
495          * Construct
496          *
497          * @param properties system descriptor properties
498          */

499         private RepositoryDescriptor(Map JavaDoc<QName, Serializable JavaDoc> properties)
500         {
501             this.properties = properties;
502         }
503         
504         /* (non-Javadoc)
505          * @see org.alfresco.service.descriptor.Descriptor#getVersionMajor()
506          */

507         public String JavaDoc getVersionMajor()
508         {
509             return getDescriptor("sys:versionMajor");
510         }
511
512         /* (non-Javadoc)
513          * @see org.alfresco.service.descriptor.Descriptor#getVersionMinor()
514          */

515         public String JavaDoc getVersionMinor()
516         {
517             return getDescriptor("sys:versionMinor");
518         }
519
520         /* (non-Javadoc)
521          * @see org.alfresco.service.descriptor.Descriptor#getVersionRevision()
522          */

523         public String JavaDoc getVersionRevision()
524         {
525             return getDescriptor("sys:versionRevision");
526         }
527
528         /* (non-Javadoc)
529          * @see org.alfresco.service.descriptor.Descriptor#getVersionLabel()
530          */

531         public String JavaDoc getVersionLabel()
532         {
533             return getDescriptor("sys:versionLabel");
534         }
535
536         /* (non-Javadoc)
537          * @see org.alfresco.service.descriptor.Descriptor#getVersion()
538          */

539         public String JavaDoc getVersion()
540         {
541             String JavaDoc version = getVersionMajor() + "." + getVersionMinor() + "." + getVersionRevision();
542             String JavaDoc label = getVersionLabel();
543             if (label != null && label.length() > 0)
544             {
545                 version += " (" + label + ")";
546             }
547             return version;
548         }
549
550         /* (non-Javadoc)
551          * @see org.alfresco.service.descriptor.Descriptor#getEdition()
552          */

553         public String JavaDoc getEdition()
554         {
555             return null;
556         }
557         
558         /*
559          * (non-Javadoc)
560          * @see org.alfresco.service.descriptor.Descriptor#getSchema()
561          */

562         public int getSchema()
563         {
564             String JavaDoc schemaStr = getDescriptor("sys:versionSchema");
565             if (schemaStr == null)
566             {
567                 return 0;
568             }
569             try
570             {
571                 int schema = Integer.parseInt(schemaStr);
572                 if (schema < 0)
573                 {
574                     throw new NumberFormatException JavaDoc();
575                 }
576                 return schema;
577             }
578             catch (NumberFormatException JavaDoc e)
579             {
580                 throw new AlfrescoRuntimeException("'version.schema' must be a positive integer");
581             }
582         }
583
584         /* (non-Javadoc)
585          * @see org.alfresco.service.descriptor.Descriptor#getDescriptorKeys()
586          */

587         public String JavaDoc[] getDescriptorKeys()
588         {
589             String JavaDoc[] keys = new String JavaDoc[properties.size()];
590             properties.keySet().toArray(keys);
591             return keys;
592         }
593
594         /* (non-Javadoc)
595          * @see org.alfresco.service.descriptor.Descriptor#getDescriptor(java.lang.String)
596          */

597         public String JavaDoc getDescriptor(String JavaDoc key)
598         {
599             String JavaDoc strValue = null;
600             QName qname = QName.createQName(key, namespaceService);
601             Serializable JavaDoc value = properties.get(qname);
602             if (value != null)
603             {
604                 strValue = value.toString();
605             }
606             return strValue;
607         }
608     }
609     
610     /**
611      * Server Descriptor whose meta-data is retrieved from run-time environment
612      */

613     private class ServerDescriptor implements Descriptor
614     {
615         /* (non-Javadoc)
616          * @see org.alfresco.service.descriptor.Descriptor#getVersionMajor()
617          */

618         public String JavaDoc getVersionMajor()
619         {
620             return serverProperties.getProperty("version.major");
621         }
622
623         /* (non-Javadoc)
624          * @see org.alfresco.service.descriptor.Descriptor#getVersionMinor()
625          */

626         public String JavaDoc getVersionMinor()
627         {
628             return serverProperties.getProperty("version.minor");
629         }
630
631         /* (non-Javadoc)
632          * @see org.alfresco.service.descriptor.Descriptor#getVersionRevision()
633          */

634         public String JavaDoc getVersionRevision()
635         {
636             return serverProperties.getProperty("version.revision");
637         }
638
639         /* (non-Javadoc)
640          * @see org.alfresco.service.descriptor.Descriptor#getVersionLabel()
641          */

642         public String JavaDoc getVersionLabel()
643         {
644             return serverProperties.getProperty("version.label");
645         }
646
647         /* (non-Javadoc)
648          * @see org.alfresco.service.descriptor.Descriptor#getVersion()
649          */

650         public String JavaDoc getVersion()
651         {
652             String JavaDoc version = getVersionMajor() + "." + getVersionMinor() + "." + getVersionRevision();
653             String JavaDoc label = getVersionLabel();
654             if (label != null && label.length() > 0)
655             {
656                 version += " (" + label + ")";
657             }
658             return version;
659         }
660
661         /* (non-Javadoc)
662          * @see org.alfresco.service.descriptor.Descriptor#getEdition()
663          */

664         public String JavaDoc getEdition()
665         {
666             return serverProperties.getProperty("version.edition");
667         }
668
669         /*
670          * (non-Javadoc)
671          * @see org.alfresco.service.descriptor.Descriptor#getSchema()
672          */

673         public int getSchema()
674         {
675             String JavaDoc schemaStr = serverProperties.getProperty("version.schema");
676             if (schemaStr == null)
677             {
678                 return 0;
679             }
680             try
681             {
682                 int schema = Integer.parseInt(schemaStr);
683                 if (schema < 0)
684                 {
685                     throw new NumberFormatException JavaDoc();
686                 }
687                 return schema;
688             }
689             catch (NumberFormatException JavaDoc e)
690             {
691                 throw new AlfrescoRuntimeException("'version.schema' must be a positive integer");
692             }
693         }
694
695         /* (non-Javadoc)
696          * @see org.alfresco.service.descriptor.Descriptor#getDescriptorKeys()
697          */

698         public String JavaDoc[] getDescriptorKeys()
699         {
700             String JavaDoc[] keys = new String JavaDoc[serverProperties.size()];
701             serverProperties.keySet().toArray(keys);
702             return keys;
703         }
704
705         /* (non-Javadoc)
706          * @see org.alfresco.service.descriptor.Descriptor#getDescriptor(java.lang.String)
707          */

708         public String JavaDoc getDescriptor(String JavaDoc key)
709         {
710             return serverProperties.getProperty(key, "");
711         }
712     }
713
714 }
715
Popular Tags