Scarab commit: svn commit: r10194 - branches/release/b21/src/java/org/tigris/scarab/services/ServiceManager.java

[email protected]
Newsgroups gmane.comp.java.scarab.cvs
Message-ID <[email protected]>
Author: dabbous
Date: 2006-07-03 02:23:44-0700
New Revision: 10194

Added:
   branches/release/b21/src/java/org/tigris/scarab/services/ServiceManager.java

Log:
fixed a bug due to parallel threading: yaafi was sometimes
initialized twices causing al sorts of unexpected behaviour
during runtime. Added the new ServiceManager java source to
version control.

Added: branches/release/b21/src/java/org/tigris/scarab/services/ServiceManager.java
Url: http://scarab.tigris.org/source/browse/scarab/branches/release/b21/src/java/org/tigris/scarab/services/ServiceManager.java?view=auto&rev=10194
==============================================================================
--- (empty file)
+++ branches/release/b21/src/java/org/tigris/scarab/services/ServiceManager.java	2006-07-03 02:23:44-0700
@@ -0,0 +1,99 @@
+package org.tigris.scarab.services;

+

+/* ====================================================================

+*

+* Copyright (c) 2006 Hussayn dabbous.

+*

+* Licensed under the

+*

+*     CollabNet/Tigris.org Apache-style license (the "License");

+*

+* you may not use this file except in compliance with the License.

+* You may obtain a copy of the License at

+*

+*     http://scarab.tigris.org/LICENSE

+*

+* Unless required by applicable law or agreed to in writing, software

+* distributed under the License is distributed on an "AS IS" BASIS,

+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 

+* implied. See the License for the specific language governing

+* permissions and limitations under the License.

+* ====================================================================

+*

+* This software consists of voluntary contributions made by many

+* individuals on behalf of CollabNet.

+*

+*/

+

+import org.apache.fulcrum.TurbineServices;

+import org.apache.fulcrum.localization.LocalizationService;

+import org.apache.turbine.services.yaaficomponent.YaafiComponentService;

+import org.tigris.scarab.tools.localization.L10NKeySet;

+import org.tigris.scarab.tools.localization.L10NMessage;

+import org.tigris.scarab.util.ScarabException;

+import org.tigris.scarab.util.ScarabRuntimeException;

+

+public class ServiceManager

+{

+    static ServiceManager instance = null;

+    

+    private ServiceManager()

+    {

+        // intentionally left blank

+    }

+

+    /**

+     * Return the one and only Servicemanager instance

+     * @return

+     */

+    public static ServiceManager getInstance()

+    {

+        if(instance == null)

+        {

+            instance = new ServiceManager();

+        }

+        return instance;

+    }

+

+    /**

+     * Lookup a service. If the service exists, but is not initialized yet,

+     * perform the initialization before returnning the service.

+     * The method is synchronized in order to avoid duplicate initializations

+     * due to possible parallel calls from multiple threads.

+     * @param serviceClass

+     * @return

+     * @throws ScarabException

+     */

+    public synchronized Object lookup(Class serviceClass)

+    {

+        Object serviceInstance;

+        YaafiComponentService yaafi = getService();

+        try{

+            serviceInstance =  yaafi.lookup(serviceClass.getName());

+        } 

+        catch (Exception e) 

+        {

+            L10NMessage msg = new L10NMessage(L10NKeySet.ExceptionGeneric, e);

+            throw new ScarabRuntimeException(msg);

+        }

+        return serviceInstance;

+    }

+    

+    public static YaafiComponentService getService()

+    {

+        YaafiComponentService yaafi = null;

+        try{

+            yaafi = (YaafiComponentService) TurbineServices.getInstance().getService(

+                    YaafiComponentService.SERVICE_NAME);

+        } 

+        catch (Exception e) 

+        {

+            L10NMessage msg = new L10NMessage(L10NKeySet.ExceptionGeneric, e);

+            throw new ScarabRuntimeException(msg);

+        }

+        return yaafi;

+        

+    }

+

+    

+}
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.