001/* 002 Licensed to the Apache Software Foundation (ASF) under one 003 or more contributor license agreements. See the NOTICE file 004 distributed with this work for additional information 005 regarding copyright ownership. The ASF licenses this file 006 to you under the Apache License, Version 2.0 (the 007 "License"); you may not use this file except in compliance 008 with the License. You may obtain a copy of the License at 009 010 http://www.apache.org/licenses/LICENSE-2.0 011 012 Unless required by applicable law or agreed to in writing, 013 software distributed under the License is distributed on an 014 "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 KIND, either express or implied. See the License for the 016 specific language governing permissions and limitations 017 under the License. 018 */ 019package org.apache.wiki.api.spi; 020 021import org.apache.wiki.util.PropertyReader; 022import org.apache.wiki.util.TextUtil; 023 024import javax.servlet.ServletContext; 025import java.nio.file.ProviderNotFoundException; 026import java.util.Properties; 027import java.util.ServiceLoader; 028 029 030public class Wiki { 031 032 private static final String PROP_PROVIDER_IMPL_ACLS = "jspwiki.provider.impl.acls"; 033 private static final String PROP_PROVIDER_IMPL_CONTENTS = "jspwiki.provider.impl.contents"; 034 private static final String PROP_PROVIDER_IMPL_CONTEXT = "jspwiki.provider.impl.context"; 035 private static final String PROP_PROVIDER_IMPL_ENGINE = "jspwiki.provider.impl.engine"; 036 private static final String PROP_PROVIDER_IMPL_SESSION = "jspwiki.provider.impl.session"; 037 private static final String DEFAULT_PROVIDER_IMPL_ACLS = "org.apache.wiki.spi.AclsSPIDefaultImpl"; 038 private static final String DEFAULT_PROVIDER_IMPL_CONTENTS = "org.apache.wiki.spi.ContentsSPIDefaultImpl"; 039 private static final String DEFAULT_PROVIDER_IMPL_CONTEXT = "org.apache.wiki.spi.ContextSPIDefaultImpl"; 040 private static final String DEFAULT_PROVIDER_IMPL_ENGINE = "org.apache.wiki.spi.EngineSPIDefaultImpl"; 041 private static final String DEFAULT_PROVIDER_IMPL_SESSION = "org.apache.wiki.spi.SessionSPIDefaultImpl"; 042 043 // default values 044 private static Properties properties = PropertyReader.getDefaultProperties(); 045 private static AclsSPI aclsSPI = getSPI( AclsSPI.class, properties, PROP_PROVIDER_IMPL_ACLS, DEFAULT_PROVIDER_IMPL_ACLS ); 046 private static ContentsSPI contentsSPI = getSPI( ContentsSPI.class, properties, PROP_PROVIDER_IMPL_CONTENTS, DEFAULT_PROVIDER_IMPL_CONTENTS ); 047 private static ContextSPI contextSPI = getSPI( ContextSPI.class, properties, PROP_PROVIDER_IMPL_CONTEXT, DEFAULT_PROVIDER_IMPL_CONTEXT ); 048 private static EngineSPI engineSPI = getSPI( EngineSPI.class, properties, PROP_PROVIDER_IMPL_ENGINE, DEFAULT_PROVIDER_IMPL_ENGINE ); 049 private static SessionSPI sessionSPI = getSPI( SessionSPI.class, properties, PROP_PROVIDER_IMPL_SESSION, DEFAULT_PROVIDER_IMPL_SESSION ); 050 051 public static Properties init( final ServletContext context ) { 052 properties = PropertyReader.loadWebAppProps( context ); 053 aclsSPI = getSPI( AclsSPI.class, properties, PROP_PROVIDER_IMPL_ACLS, DEFAULT_PROVIDER_IMPL_ACLS ); 054 contentsSPI = getSPI( ContentsSPI.class, properties, PROP_PROVIDER_IMPL_CONTENTS, DEFAULT_PROVIDER_IMPL_CONTENTS ); 055 contextSPI = getSPI( ContextSPI.class, properties, PROP_PROVIDER_IMPL_CONTEXT, DEFAULT_PROVIDER_IMPL_CONTEXT ); 056 engineSPI = getSPI( EngineSPI.class, properties, PROP_PROVIDER_IMPL_ENGINE, DEFAULT_PROVIDER_IMPL_ENGINE ); 057 sessionSPI = getSPI( SessionSPI.class, properties, PROP_PROVIDER_IMPL_SESSION, DEFAULT_PROVIDER_IMPL_SESSION ); 058 return properties; 059 } 060 061 /** 062 * Access to {@link AclsSPI} operations. 063 * 064 * @return {@link AclsSPI} operations. 065 */ 066 public static AclsDSL acls() { 067 return new AclsDSL( aclsSPI ); 068 } 069 070 /** 071 * Access to {@link ContentsSPI} operations. 072 * 073 * @return {@link ContentsSPI} operations. 074 */ 075 public static ContentsDSL contents() { 076 return new ContentsDSL( contentsSPI ); 077 } 078 079 /** 080 * Access to {@link ContextSPI} operations. 081 * 082 * @return {@link ContextSPI} operations. 083 */ 084 public static ContextDSL context() { 085 return new ContextDSL( contextSPI ); 086 } 087 088 /** 089 * Access to {@link EngineSPI} operations. 090 * 091 * @return {@link EngineSPI} operations. 092 */ 093 public static EngineDSL engine() { 094 return new EngineDSL( engineSPI ); 095 } 096 097 /** 098 * Access to {@link SessionSPI} operations. 099 * 100 * @return {@link SessionSPI} operations. 101 */ 102 public static SessionDSL session() { 103 return new SessionDSL( sessionSPI ); 104 } 105 106 static < SPI > SPI getSPI( final Class< SPI > spi, final Properties props, final String prop, final String defValue ) { 107 final String providerImpl = TextUtil.getStringProperty( props, prop, defValue ); 108 final ServiceLoader< SPI > loader = ServiceLoader.load( spi ); 109 for( final SPI provider : loader ) { 110 if( providerImpl.equals( provider.getClass().getName() ) ) { 111 return provider; 112 } 113 } 114 throw new ProviderNotFoundException( spi.getName() + " provider not found" ); 115 } 116 117}