001/* 002 * Syncany, www.syncany.org 003 * Copyright (C) 2011-2016 Philipp C. Heckel <philipp.heckel@gmail.com> 004 * 005 * This program is free software: you can redistribute it and/or modify 006 * it under the terms of the GNU General Public License as published by 007 * the Free Software Foundation, either version 3 of the License, or 008 * (at your option) any later version. 009 * 010 * This program is distributed in the hope that it will be useful, 011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 013 * GNU General Public License for more details. 014 * 015 * You should have received a copy of the GNU General Public License 016 * along with this program. If not, see <http://www.gnu.org/licenses/>. 017 */ 018package org.syncany.plugins.transfer; 019 020import java.text.MessageFormat; 021import java.util.regex.Matcher; 022import java.util.regex.Pattern; 023 024import org.syncany.plugins.Plugin; 025import com.google.common.base.CaseFormat; 026 027/** 028 * Helper class for {@link TransferPlugin}s, using to retrieve 029 * the required transfer plugin classes -- namely {@link TransferSettings}, 030 * {@link TransferManager} and {@link TransferPlugin}. 031 * 032 * @author Christian Roth (christian.roth@port17.de) 033 */ 034public abstract class TransferPluginUtil { 035 private static final String PLUGIN_PACKAGE_NAME_REGEX = "^" + Plugin.class.getPackage().getName().replace(".", "\\.") + "\\.([a-z0-9_]+)$"; 036 private static final Pattern PLUGIN_PACKAGE_NAME_PATTERN = Pattern.compile(PLUGIN_PACKAGE_NAME_REGEX); 037 038 private static final String PLUGIN_PACKAGE_NAME = Plugin.class.getPackage().getName() + ".{0}."; 039 private static final String PLUGIN_TRANSFER_SETTINGS_CLASS_NAME = PLUGIN_PACKAGE_NAME + "{1}" + TransferSettings.class.getSimpleName(); 040 private static final String PLUGIN_TRANSFER_MANAGER_CLASS_NAME = PLUGIN_PACKAGE_NAME + "{1}" + TransferManager.class.getSimpleName(); 041 private static final String PLUGIN_TRANSFER_PLUGIN_CLASS_NAME = PLUGIN_PACKAGE_NAME + "{1}" + TransferPlugin.class.getSimpleName(); 042 043 /** 044 * Determines the {@link TransferSettings} class for a given 045 * {@link TransferPlugin} class. 046 */ 047 public static Class<? extends TransferSettings> getTransferSettingsClass(Class<? extends TransferPlugin> transferPluginClass) { 048 String pluginNameIdentifier = TransferPluginUtil.getPluginPackageName(transferPluginClass); 049 050 if (pluginNameIdentifier == null) { 051 throw new RuntimeException("There are no valid transfer settings attached to that plugin (" + transferPluginClass.getName() + ")"); 052 } 053 else { 054 try { 055 String pluginPackageIdentifier = CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, pluginNameIdentifier); 056 String transferSettingsClassName = MessageFormat.format(PLUGIN_TRANSFER_SETTINGS_CLASS_NAME, pluginPackageIdentifier, 057 pluginNameIdentifier); 058 059 return Class.forName(transferSettingsClassName).asSubclass(TransferSettings.class); 060 } 061 catch (Exception e) { 062 throw new RuntimeException("Cannot find matching transfer settings class for plugin (" + transferPluginClass.getName() + ")"); 063 } 064 } 065 } 066 067 /** 068 * Determines the {@link TransferManager} class for a given 069 * {@link TransferPlugin} class. 070 */ 071 public static Class<? extends TransferManager> getTransferManagerClass(Class<? extends TransferPlugin> transferPluginClass) { 072 String pluginNameIdentifier = TransferPluginUtil.getPluginPackageName(transferPluginClass); 073 074 if (pluginNameIdentifier == null) { 075 throw new RuntimeException("There are no valid transfer manager attached to that plugin (" + transferPluginClass.getName() + ")"); 076 } 077 else { 078 try { 079 String pluginPackageIdentifier = CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, pluginNameIdentifier); 080 String transferManagerClassName = MessageFormat.format(PLUGIN_TRANSFER_MANAGER_CLASS_NAME, pluginPackageIdentifier, 081 pluginNameIdentifier); 082 return Class.forName(transferManagerClassName).asSubclass(TransferManager.class); 083 } 084 catch (Exception e) { 085 throw new RuntimeException("Cannot find matching transfer manager class for plugin (" + transferPluginClass.getName() + ")"); 086 } 087 } 088 } 089 090 /** 091 * Determines the {@link TransferPlugin} class for a given 092 * {@link TransferSettings} class. 093 */ 094 public static Class<? extends TransferPlugin> getTransferPluginClass(Class<? extends TransferSettings> transferSettingsClass) { 095 String pluginNameIdentifier = TransferPluginUtil.getPluginPackageName(transferSettingsClass); 096 097 if (pluginNameIdentifier == null) { 098 throw new RuntimeException("The transfer settings are orphan (" + transferSettingsClass.getName() + ")"); 099 } 100 else { 101 try { 102 String pluginPackageIdentifier = CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, pluginNameIdentifier); 103 String transferPluginClassName = MessageFormat.format(PLUGIN_TRANSFER_PLUGIN_CLASS_NAME, pluginPackageIdentifier, 104 pluginNameIdentifier); 105 return Class.forName(transferPluginClassName).asSubclass(TransferPlugin.class); 106 } 107 catch (Exception e) { 108 throw new RuntimeException("Cannot find matching transfer plugin class for plugin settings (" + transferSettingsClass.getName() + ")"); 109 } 110 } 111 } 112 113 private static String getPluginPackageName(Class<?> clazz) { 114 Matcher matcher = PLUGIN_PACKAGE_NAME_PATTERN.matcher(clazz.getPackage().getName()); 115 116 if (matcher.matches()) { 117 String pluginPackageName = matcher.group(1); 118 return CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, pluginPackageName); 119 } 120 121 return null; 122 } 123}