blob: ac74597ceda2dc3da41a3b7c4dca2ab25424ae90 [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2008 Innoopract Informationssysteme GmbH.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Innoopract Informationssysteme GmbH - initial API and implementation
******************************************************************************/
package org.eclipse.epp.wizard.internal;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import org.apache.log4j.Logger;
public class ExternalServiceProviderConfiguration {
static Logger logger = Logger.getLogger( ExternalServiceProviderConfiguration.class );
private static final String URL = "url";
private static final String ICON = "icon";
private static final String DESCRIPTION = "description";
private static final String LABEL = "label";
private static final String PROPERTIES_EXTENSION = ".properties";
public static class Entry {
public String label;
public String description;
public File icon;
public String getLabel() {
return label;
}
public String getDescription() {
return description;
}
public File getIcon() {
return icon;
}
public String getUrlTemplate() {
return urlTemplate;
}
public Entry( String label,
File icon,
String description,
String urlTemplate )
{
super();
this.description = description;
this.icon = icon;
this.label = label;
this.urlTemplate = urlTemplate;
}
public String urlTemplate;
}
private List<Entry> entries = new ArrayList<Entry>();
public ExternalServiceProviderConfiguration( File externalServiceProviderDirectory )
{
for( File propertiesFile : externalServiceProviderDirectory.listFiles() ) {
if( propertiesFile.getName().endsWith( PROPERTIES_EXTENSION ) ) {
FileInputStream inStream = null;
try {
Properties properties = new Properties();
inStream = new FileInputStream( propertiesFile );
properties.load( inStream );
String label = getMandatoryProperty( properties, LABEL );
String description = getMandatoryProperty( properties, DESCRIPTION );
String icon = getMandatoryProperty( properties, ICON );
String url = getMandatoryProperty( properties, URL );
Entry entry = new Entry( label,
new File( externalServiceProviderDirectory,
icon ),
description,
url );
entries.add( entry );
} catch( Exception exc ) {
throw new RuntimeException( "Could not load external service provider configuration file'"
+ propertiesFile.getAbsolutePath()
+ "'",
exc );
} finally {
if( inStream != null ) {
try {
inStream.close();
} catch( IOException e ) {
logger.warn( "Could not close file '" + propertiesFile, e );
}
}
}
}
}
}
private String getMandatoryProperty( Properties properties, String string ) {
String result = properties.getProperty( string );
if( result == null ) {
throw new RuntimeException( "External service provider key '"
+ string
+ "' not found in properties" );
}
return result;
}
public List<Entry> getEntries() {
return entries;
}
}