public void initialize(IRuntimeServices rs)
        {
            runtimeServices = rs;

            int maxSize = runtimeServices.GetInt(RuntimeConstants.RESOURCE_MANAGER_DEFAULTCACHE_SIZE, 89);
            if (maxSize > 0)
            {
                // Create a whole new Map here to avoid hanging on to a
                // handle to the unsynch'd LRUMap for our lifetime.
                LRUMap lruCache = LRUMap.Synchronized(new LRUMap(maxSize));
                lruCache.AddAll(cache);
                cache = lruCache;
            }

            runtimeServices.Info(string.Format("ResourceCache : initialized. ({0})", GetType()));
        }
Beispiel #2
0
        public void initialize(IRuntimeServices rs)
        {
            runtimeServices = rs;

            int maxSize = runtimeServices.GetInt(RuntimeConstants.RESOURCE_MANAGER_DEFAULTCACHE_SIZE, 89);

            if (maxSize > 0)
            {
                // Create a whole new Map here to avoid hanging on to a
                // handle to the unsynch'd LRUMap for our lifetime.
                LRUMap lruCache = LRUMap.Synchronized(new LRUMap(maxSize));
                lruCache.AddAll(cache);
                cache = lruCache;
            }

            runtimeServices.Info(string.Format("ResourceCache : initialized. ({0})", GetType()));
        }
        public static ResourceLoader getLoader(IRuntimeServices rs, string loaderClassName)
        {
            ResourceLoader result;

            try
            {
                loaderClassName = loaderClassName.Replace(';', ',');
                Type           type           = Type.GetType(loaderClassName);
                object         obj            = Activator.CreateInstance(type);
                ResourceLoader resourceLoader = (ResourceLoader)obj;
                rs.Info("Resource Loader Instantiated: " + resourceLoader.GetType().FullName);
                result = resourceLoader;
            }
            catch (System.Exception e)
            {
                rs.Error("Problem instantiating the template loader.\nLook at your properties file and make sure the\nname of the template loader is correct. Here is the\nerror: " + StringUtils.StackTrace(e));
                throw new System.Exception("Problem initializing template loader: " + loaderClassName + "\nError is: " + StringUtils.StackTrace(e));
            }
            return(result);
        }
		/// <summary>
		/// Gets the loader specified in the configuration file.
		/// </summary>
		/// <returns>TemplateLoader</returns>
		public static ResourceLoader getLoader(IRuntimeServices rs, String loaderClassName)
		{
			try
			{
				// since properties are parsed into arrays with commas, 
				// something else needed to be used
				loaderClassName = loaderClassName.Replace(';', ',');
				Type loaderType = Type.GetType(loaderClassName);
				Object o = Activator.CreateInstance(loaderType);
				ResourceLoader loader = (ResourceLoader) o;

				rs.Info(string.Format("Resource Loader Instantiated: {0}", loader.GetType().FullName));

				return loader;
			}
			catch(System.Exception e)
			{
				rs.Error(
					string.Format(
						"Problem instantiating the template loader.\nLook at your properties file and make sure the\nname of the template loader is correct. Here is the\nerror: {0}",
						e));
				throw new System.Exception(string.Format("Problem initializing template loader: {0}\nError is: {1}", loaderClassName, e));
			}
		}
Beispiel #5
0
        /// <summary>
        /// Gets the loader specified in the configuration file.
        /// </summary>
        /// <returns>TemplateLoader</returns>
        public static ResourceLoader getLoader(IRuntimeServices rs, String loaderClassName)
        {
            try
            {
                // since properties are parsed into arrays with commas,
                // something else needed to be used
                loaderClassName = loaderClassName.Replace(';', ',');
                Type           loaderType = Type.GetType(loaderClassName);
                Object         o          = Activator.CreateInstance(loaderType);
                ResourceLoader loader     = (ResourceLoader)o;

                rs.Info(string.Format("Resource Loader Instantiated: {0}", loader.GetType().FullName));

                return(loader);
            }
            catch (System.Exception e)
            {
                rs.Error(
                    string.Format(
                        "Problem instantiating the template loader.\nLook at your properties file and make sure the\nname of the template loader is correct. Here is the\nerror: {0}",
                        e));
                throw new System.Exception(string.Format("Problem initializing template loader: {0}\nError is: {1}", loaderClassName, e));
            }
        }
Beispiel #6
0
        /// <summary>  Creates a new logging system or returns an existing one
        /// specified by the application.
        /// </summary>
        public static ILogSystem CreateLogSystem(IRuntimeServices runtimeServices)
        {
            ILogSystem logSystem;
            // if a logSystem was set as a configuration value, use that.
            // This is any class the user specifies.
            Object o = runtimeServices.GetProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM);

            logSystem = o as ILogSystem;
            if (logSystem != null)
            {
                logSystem.Init(runtimeServices);

                return(logSystem);
            }

            // otherwise, see if a class was specified.  You
            // can put multiple classes, and we use the first one we find.
            //
            // Note that the default value of this property contains both the
            // AvalonLogSystem and the SimpleLog4JLogSystem for convenience -
            // so we use whichever we find.
            IList  classes = new ArrayList();
            Object obj     = runtimeServices.GetProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS);

            // we might have a list, or not - so check
            if (obj is IList)
            {
                classes = (IList)obj;
            }
            else if (obj is String)
            {
                classes.Add(obj);
            }

            // now run through the list, trying each.  It's ok to
            // fail with a class not found, as we do this to also
            // search out a default simple file logger
            foreach (String className in classes)
            {
                if (className != null && className.Length > 0)
                {
                    runtimeServices.Info(string.Format("Trying to use logger class {0}", className));

                    try
                    {
                        Type type = Type.GetType(className);
                        o         = Activator.CreateInstance(type);
                        logSystem = o as ILogSystem;
                        if (logSystem == null)
                        {
                            runtimeServices.Error(string.Format("The specified logger class {0} isn't a valid LogSystem", className));
                        }
                        else
                        {
                            logSystem.Init(runtimeServices);

                            runtimeServices.Info(string.Format("Using logger class {0}", className));

                            return(logSystem);
                        }
                    }
                    catch (ApplicationException applicationException)
                    {
                        runtimeServices.Debug(
                            string.Format("Couldn't find class {0} or necessary supporting classes in classpath. Exception : {1}", className,
                                          applicationException));
                    }
                }
            }

            // if the above failed, then we are in deep doo-doo, as the
            // above means that either the user specified a logging class
            // that we can't find, there weren't the necessary
            // dependencies in the classpath for it, or there were no
            // dependencies for the default logger.
            // Since we really don't know,
            // then take a wack at the log4net as a last resort.
            try
            {
                logSystem = new NullLogSystem();
                logSystem.Init(runtimeServices);
            }
            catch (ApplicationException applicationException)
            {
                String error =
                    string.Format(
                        "PANIC : NVelocity cannot find any of the specified or default logging systems in the classpath, or the classpath doesn't contain the necessary classes to support them. Please consult the documentation regarding logging. Exception : {0}",
                        applicationException);

                Console.Out.WriteLine(error);
                Console.Error.WriteLine(error);

                throw;
            }

            runtimeServices.Info("Using log4net as logger of final resort.");

            return(logSystem);
        }
Beispiel #7
0
		/// <summary>  Creates a new logging system or returns an existing one
		/// specified by the application.
		/// </summary>
		public static ILogSystem CreateLogSystem(IRuntimeServices runtimeServices)
		{
			ILogSystem logSystem;
			// if a logSystem was set as a configuration value, use that.
			// This is any class the user specifies.
			Object o = runtimeServices.GetProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM);
			logSystem = o as ILogSystem;
			if (logSystem != null)
			{
				logSystem.Init(runtimeServices);

				return logSystem;
			}

			// otherwise, see if a class was specified.  You
			// can put multiple classes, and we use the first one we find.
			//
			// Note that the default value of this property contains both the
			// AvalonLogSystem and the SimpleLog4JLogSystem for convenience -
			// so we use whichever we find.
			IList classes = new ArrayList();
			Object obj = runtimeServices.GetProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS);

			// we might have a list, or not - so check
			if (obj is IList)
			{
				classes = (IList) obj;
			}
			else if (obj is String)
			{
				classes.Add(obj);
			}

			// now run through the list, trying each.  It's ok to
			// fail with a class not found, as we do this to also
			// search out a default simple file logger
			foreach(String className in classes)
			{
				if (className != null && className.Length > 0)
				{
					runtimeServices.Info(string.Format("Trying to use logger class {0}", className));

					try
					{
						Type type = Type.GetType(className);
						o = Activator.CreateInstance(type);
						logSystem = o as ILogSystem;
						if (logSystem == null)
						{
							runtimeServices.Error(string.Format("The specified logger class {0} isn't a valid LogSystem", className));
						}
						else
						{
							logSystem.Init(runtimeServices);

							runtimeServices.Info(string.Format("Using logger class {0}", className));

							return logSystem;
						}
					}
					catch(ApplicationException applicationException)
					{
						runtimeServices.Debug(
							string.Format("Couldn't find class {0} or necessary supporting classes in classpath. Exception : {1}", className,
							              applicationException));
					}
				}
			}

			// if the above failed, then we are in deep doo-doo, as the
			// above means that either the user specified a logging class
			// that we can't find, there weren't the necessary
			// dependencies in the classpath for it, or there were no
			// dependencies for the default logger.
			// Since we really don't know,
			// then take a wack at the log4net as a last resort.
			try
			{
				logSystem = new NullLogSystem();
				logSystem.Init(runtimeServices);
			}
			catch(ApplicationException applicationException)
			{
				String error =
					string.Format(
						"PANIC : NVelocity cannot find any of the specified or default logging systems in the classpath, or the classpath doesn't contain the necessary classes to support them. Please consult the documentation regarding logging. Exception : {0}",
						applicationException);

				Console.Out.WriteLine(error);
				Console.Error.WriteLine(error);

				throw;
			}

			runtimeServices.Info("Using log4net as logger of final resort.");

			return logSystem;
		}
		/// <summary>
		/// Initialize the ResourceManager.
		/// </summary>
		public void Initialize(IRuntimeServices rs)
		{
			runtimeServices = rs;

			runtimeServices.Info(string.Format("Default ResourceManager initializing. ({0})", GetType()));

			ResourceLoader resourceLoader;

			AssembleResourceLoaderInitializers();

			for(int i = 0; i < sourceInitializerList.Count; i++)
			{
				ExtendedProperties configuration = (ExtendedProperties) sourceInitializerList[i];
				String loaderClass = configuration.GetString("class");

				if (loaderClass == null)
				{
					runtimeServices.Error(
						string.Format(
							"Unable to find '{0}.resource.loader.class' specification in configuration. This is a critical value.  Please adjust configuration.",
							configuration.GetString(RESOURCE_LOADER_IDENTIFIER)));
					continue;
				}

				resourceLoader = ResourceLoaderFactory.getLoader(runtimeServices, loaderClass);
				resourceLoader.CommonInit(runtimeServices, configuration);
				resourceLoader.Init(configuration);
				resourceLoaders.Add(resourceLoader);
			}

			// now see if this is overridden by configuration
			logWhenFound = runtimeServices.GetBoolean(RuntimeConstants.RESOURCE_MANAGER_LOGWHENFOUND, true);

			// now, is a global cache specified?
			String resourceManagerCacheClassName = runtimeServices.GetString(RuntimeConstants.RESOURCE_MANAGER_CACHE_CLASS);
			Object o = null;

			if (resourceManagerCacheClassName != null && resourceManagerCacheClassName.Length > 0)
			{
				try
				{
					Type type = Type.GetType(resourceManagerCacheClassName);
					o = Activator.CreateInstance(type);
				}
				catch(System.Exception)
				{
					String err =
						string.Format(
							"The specified class for ResourceCache ({0}) does not exist (or is not accessible to the current classLoader).",
							resourceManagerCacheClassName);
					runtimeServices.Error(err);
					o = null;
				}

				if (!(o is ResourceCache))
				{
					String err =
						string.Format(
							"The specified class for ResourceCache ({0}) does not implement NVelocity.Runtime.Resource.ResourceCache. Using default ResourceCache implementation.",
							resourceManagerCacheClassName);
					runtimeServices.Error(err);
					o = null;
				}
			}

			// if we didn't get through that, just use the default.
			if (o == null)
			{
				o = new ResourceCacheImpl();
			}

			globalCache = (ResourceCache) o;
			globalCache.initialize(runtimeServices);
			runtimeServices.Info("Default ResourceManager initialization complete.");
		}
Beispiel #9
0
        /// <summary>
        /// Initialize the ResourceManager.
        /// </summary>
        public void Initialize(IRuntimeServices rs)
        {
            runtimeServices = rs;

            runtimeServices.Info(string.Format("Default ResourceManager initializing. ({0})", GetType()));

            ResourceLoader resourceLoader;

            AssembleResourceLoaderInitializers();

            for (int i = 0; i < sourceInitializerList.Count; i++)
            {
                ExtendedProperties configuration = (ExtendedProperties)sourceInitializerList[i];
                String             loaderClass   = configuration.GetString("class");

                if (loaderClass == null)
                {
                    runtimeServices.Error(
                        string.Format(
                            "Unable to find '{0}.resource.loader.class' specification in configuration. This is a critical value.  Please adjust configuration.",
                            configuration.GetString(RESOURCE_LOADER_IDENTIFIER)));
                    continue;
                }

                resourceLoader = ResourceLoaderFactory.getLoader(runtimeServices, loaderClass);
                resourceLoader.CommonInit(runtimeServices, configuration);
                resourceLoader.Init(configuration);
                resourceLoaders.Add(resourceLoader);
            }

            // now see if this is overridden by configuration
            logWhenFound = runtimeServices.GetBoolean(RuntimeConstants.RESOURCE_MANAGER_LOGWHENFOUND, true);

            // now, is a global cache specified?
            String resourceManagerCacheClassName = runtimeServices.GetString(RuntimeConstants.RESOURCE_MANAGER_CACHE_CLASS);
            Object o = null;

            if (resourceManagerCacheClassName != null && resourceManagerCacheClassName.Length > 0)
            {
                try
                {
                    Type type = Type.GetType(resourceManagerCacheClassName);
                    o = Activator.CreateInstance(type);
                }
                catch (Exception)
                {
                    String err =
                        string.Format(
                            "The specified class for ResourceCache ({0}) does not exist (or is not accessible to the current classLoader).",
                            resourceManagerCacheClassName);
                    runtimeServices.Error(err);
                    o = null;
                }

                if (!(o is ResourceCache))
                {
                    String err =
                        string.Format(
                            "The specified class for ResourceCache ({0}) does not implement NVelocity.Runtime.Resource.ResourceCache. Using default ResourceCache implementation.",
                            resourceManagerCacheClassName);
                    runtimeServices.Error(err);
                    o = null;
                }
            }

            // if we didn't get through that, just use the default.
            if (o == null)
            {
                o = new ResourceCacheImpl();
            }

            globalCache = (ResourceCache)o;
            globalCache.initialize(runtimeServices);
            runtimeServices.Info("Default ResourceManager initialization complete.");
        }
Beispiel #10
0
        /// <summary>
        /// Loads a resource from the current set of resource loaders
        /// </summary>
        /// <param name="resourceName">The name of the resource to retrieve.</param>
        /// <param name="resourceType">The type of resource (<code>Template</code>,
        /// <code>Content</code>, etc.).
        /// </param>
        /// <param name="encoding"> The character encoding to use.</param>
        /// <returns>Resource with the template parsed and ready.
        /// @throws ResourceNotFoundException if template not found
        /// from any available source.
        /// @throws ParseErrorException if template cannot be parsed due
        /// to syntax (or other) error.
        /// @throws Exception if a problem in parse
        /// </returns>
        protected internal Resource LoadResource(String resourceName, ResourceType resourceType, String encoding)
        {
            Resource resource = ResourceFactory.GetResource(resourceName, resourceType);

            resource.RuntimeServices = runtimeServices;

            resource.Name     = resourceName;
            resource.Encoding = encoding;

            /*
             * Now we have to try to find the appropriate
             * loader for this resource. We have to cycle through
             * the list of available resource loaders and see
             * which one gives us a stream that we can use to
             * make a resource with.
             */

            long howOldItWas = 0;             // Initialize to avoid warnings

            ResourceLoader resourceLoader = null;

            for (int i = 0; i < resourceLoaders.Count; i++)
            {
                resourceLoader          = (ResourceLoader)resourceLoaders[i];
                resource.ResourceLoader = resourceLoader;

                /*
                 *  catch the ResourceNotFound exception
                 *  as that is ok in our new multi-loader environment
                 */

                try
                {
                    if (resource.Process())
                    {
                        /*
                         *  FIXME  (gmj)
                         *  moved in here - technically still
                         *  a problem - but the resource needs to be
                         *  processed before the loader can figure
                         *  it out due to to the new
                         *  multi-path support - will revisit and fix
                         */

                        if (logWhenFound)
                        {
                            runtimeServices.Info(
                                string.Format("ResourceManager : found {0} with loader {1}", resourceName, resourceLoader.ClassName));
                        }

                        howOldItWas = resourceLoader.GetLastModified(resource);
                        break;
                    }
                }
                catch (ResourceNotFoundException)
                {
                    /*
                     *  that's ok - it's possible to fail in
                     *  multi-loader environment
                     */
                }
            }

            /*
             * Return null if we can't find a resource.
             */
            if (resource.Data == null)
            {
                throw new ResourceNotFoundException(string.Format("Unable to find resource '{0}'", resourceName));
            }

            /*
             *  some final cleanup
             */

            resource.LastModified = howOldItWas;

            resource.ModificationCheckInterval = resourceLoader.ModificationCheckInterval;

            resource.Touch();

            return(resource);
        }