1   package com.atlassian.plugins.codegen.modules.common.licensing;
2   
3   import com.atlassian.plugins.codegen.ArtifactDependency;
4   import com.atlassian.plugins.codegen.BundleInstruction;
5   import com.atlassian.plugins.codegen.ClassId;
6   import com.atlassian.plugins.codegen.ComponentDeclaration;
7   import com.atlassian.plugins.codegen.ComponentImport;
8   import com.atlassian.plugins.codegen.PluginArtifact;
9   import com.atlassian.plugins.codegen.PluginProjectChangeset;
10  import com.atlassian.plugins.codegen.VersionId;
11  import com.atlassian.plugins.codegen.annotations.BambooPluginModuleCreator;
12  import com.atlassian.plugins.codegen.annotations.ConfluencePluginModuleCreator;
13  import com.atlassian.plugins.codegen.annotations.CrowdPluginModuleCreator;
14  import com.atlassian.plugins.codegen.annotations.FeCruPluginModuleCreator;
15  import com.atlassian.plugins.codegen.annotations.JiraPluginModuleCreator;
16  import com.atlassian.plugins.codegen.annotations.RefAppPluginModuleCreator;
17  import com.atlassian.plugins.codegen.modules.AbstractPluginModuleCreator;
18  import com.atlassian.plugins.codegen.modules.BasicClassModuleProperties;
19  import com.atlassian.plugins.codegen.modules.common.servlet.ServletModuleCreator;
20  import com.atlassian.plugins.codegen.modules.common.servlet.ServletProperties;
21  
22  import static com.atlassian.fugue.Option.some;
23  import static com.atlassian.plugins.codegen.ArtifactDependency.dependency;
24  import static com.atlassian.plugins.codegen.ArtifactDependency.Scope.COMPILE;
25  import static com.atlassian.plugins.codegen.ArtifactDependency.Scope.PROVIDED;
26  import static com.atlassian.plugins.codegen.ArtifactId.artifactId;
27  import static com.atlassian.plugins.codegen.BundleInstruction.dynamicImportPackage;
28  import static com.atlassian.plugins.codegen.BundleInstruction.privatePackage;
29  import static com.atlassian.plugins.codegen.ClassId.fullyQualified;
30  import static com.atlassian.plugins.codegen.ComponentDeclaration.componentDeclaration;
31  import static com.atlassian.plugins.codegen.ComponentImport.componentImport;
32  import static com.atlassian.plugins.codegen.MavenPlugin.mavenPlugin;
33  import static com.atlassian.plugins.codegen.PluginArtifact.pluginArtifact;
34  import static com.atlassian.plugins.codegen.PluginArtifact.ArtifactType.BUNDLED_ARTIFACT;
35  import static com.atlassian.plugins.codegen.PluginParameter.pluginParameter;
36  import static com.atlassian.plugins.codegen.ResourceFile.resourceFile;
37  import static com.atlassian.plugins.codegen.VersionId.noVersion;
38  import static com.atlassian.plugins.codegen.VersionId.version;
39  import static com.atlassian.plugins.codegen.VersionId.versionProperty;
40  import static com.atlassian.plugins.codegen.modules.Dependencies.APPLICATION_PROPERTIES_IMPORT;
41  import static com.atlassian.plugins.codegen.modules.Dependencies.COMMONS_LANG;
42  import static com.atlassian.plugins.codegen.modules.Dependencies.I18N_RESOLVER_IMPORT;
43  import static com.atlassian.plugins.codegen.modules.Dependencies.SAL_API;
44  import static com.atlassian.plugins.codegen.modules.Dependencies.TEMPLATE_RENDERER_API;
45  import static com.atlassian.plugins.codegen.modules.Dependencies.TEMPLATE_RENDERER_IMPORT;
46  
47  /**
48   * @since 3.9
49   */
50  @RefAppPluginModuleCreator
51  @JiraPluginModuleCreator
52  @ConfluencePluginModuleCreator
53  @BambooPluginModuleCreator
54  @FeCruPluginModuleCreator
55  @CrowdPluginModuleCreator
56  public class LicensingUpm1CompatibleModuleCreator extends AbstractPluginModuleCreator<LicensingProperties>
57  {
58      public static final String MODULE_NAME = "Licensing API Support";
59  
60      public static final String LICENSE_SERVLET_TEMPLATE = "templates/common/licensing/LicenseServlet.java.vtl";
61      public static final String HELLO_WORLD_SERVLET_TEMPLATE = "templates/common/licensing/HelloWorldServlet.java.vtl";
62      public static final String LICENSE_SERVLET_VELOCITY_TEMPLATE = "templates/common/licensing/license-admin.vm";
63      public static final String PROPERTIES_TEMPLATE = "templates/common/licensing/license-admin.properties.vtl";
64      
65      public static final ClassId LICENSE_STORAGE_MANAGER_CLASS = fullyQualified("com.atlassian.upm.license.storage.lib.ThirdPartyPluginLicenseStorageManagerImpl");
66      public static final ClassId PLUGIN_INSTALLER_CLASS = fullyQualified("com.atlassian.upm.license.storage.lib.PluginLicenseStoragePluginInstaller");
67      public static final ClassId MARKETPLACE_URI_FACTORY_CLASS = fullyQualified("com.atlassian.upm.license.storage.lib.AtlassianMarketplaceUriFactoryImpl");
68      
69      public static final String LICENSE_SERVLET_NAME_I18N_KEY = "license-servlet.name";
70      public static final String LICENSE_SERVLET_DESCRIPTION_I18N_KEY = "license-servlet.description";
71      
72      public static final String HELLO_WORLD_SERVLET_CLASS_NAME = "LicenseHelloWorldServlet";
73      public static final String HELLO_WORLD_SERVLET_NAME_I18N_KEY = "license-hello-world-servlet.name";
74      public static final String HELLO_WORLD_SERVLET_DESCRIPTION_I18N_KEY = "license-hello-world-servlet.description";
75      
76      public static final String LICENSE_API_VERSION = "2.1";
77      public static final VersionId LICENSE_API_VERSION_PROPERTY = versionProperty("upm.license.compatibility.version", LICENSE_API_VERSION);
78  
79      public static final String LICENSE_API_IMPORT_VERSION = "2.0.1";
80      
81      public static final ArtifactDependency[] DEPENDENCIES = {
82          dependency("com.atlassian.upm", "plugin-license-storage-lib", LICENSE_API_VERSION_PROPERTY, COMPILE),
83          dependency("com.atlassian.upm", "plugin-license-storage-plugin", LICENSE_API_VERSION_PROPERTY, PROVIDED),
84          dependency("com.atlassian.upm", "licensing-api", LICENSE_API_VERSION_PROPERTY, PROVIDED),
85          dependency("com.atlassian.upm", "upm-api", LICENSE_API_VERSION_PROPERTY, PROVIDED),
86          SAL_API,
87          TEMPLATE_RENDERER_API,
88          COMMONS_LANG,
89          dependency("org.springframework.osgi", "spring-osgi-core", "1.1.3", PROVIDED)
90      };
91      
92      public static final PluginArtifact[] BUNDLED_ARTIFACTS = {
93          pluginArtifact(BUNDLED_ARTIFACT, artifactId("com.atlassian.upm", "atlassian-universal-plugin-manager-plugin"), version("1.6.3")),
94          pluginArtifact(BUNDLED_ARTIFACT, artifactId("com.atlassian.upm", "plugin-license-storage-plugin"), LICENSE_API_VERSION_PROPERTY)
95      };
96      
97      public static final BundleInstruction[] BUNDLE_INSTRUCTIONS = {
98          privatePackage("com.atlassian.upm.license.storage.lib*"),
99          dynamicImportPackage("com.atlassian.upm.api.license", LICENSE_API_IMPORT_VERSION),
100         dynamicImportPackage("com.atlassian.upm.api.license.entity", LICENSE_API_IMPORT_VERSION),
101         dynamicImportPackage("com.atlassian.upm.api.util", LICENSE_API_IMPORT_VERSION),
102         dynamicImportPackage("com.atlassian.upm.license.storage.plugin", LICENSE_API_IMPORT_VERSION)
103     };
104     
105     public static final ComponentImport[] COMPONENT_IMPORTS = {
106         componentImport(fullyQualified("com.atlassian.plugin.PluginAccessor")),
107         componentImport(fullyQualified("com.atlassian.plugin.PluginController")),
108         componentImport(fullyQualified("com.atlassian.sal.api.transaction.TransactionTemplate")).key(some("txTemplate")),
109         APPLICATION_PROPERTIES_IMPORT,
110         TEMPLATE_RENDERER_IMPORT,
111         componentImport(fullyQualified("com.atlassian.sal.api.pluginsettings.PluginSettingsFactory")),
112         componentImport(fullyQualified("com.atlassian.sal.api.auth.LoginUriProvider")),
113         componentImport(fullyQualified("com.atlassian.sal.api.user.UserManager")),
114         I18N_RESOLVER_IMPORT
115     };
116     
117     public static final ComponentDeclaration[] COMPONENTS = {
118         componentDeclaration(LICENSE_STORAGE_MANAGER_CLASS, "thirdPartyPluginLicenseStorageManager"),
119         componentDeclaration(PLUGIN_INSTALLER_CLASS, "pluginLicenseStoragePluginInstaller"),
120         componentDeclaration(MARKETPLACE_URI_FACTORY_CLASS, "atlassianMarketplaceUriFactory")
121     };
122     
123     public static final String MAVEN_DEPENDENCY_PLUGIN_ID = "maven-dependency-plugin";
124     public static final String MAVEN_DEPENDENCY_PLUGIN_CONFIG = "<executions>" +
125             "<execution>" +
126                 "<id>copy-storage-plugin</id>" +
127                 "<phase>process-resources</phase>" +
128                 "<goals><goal>copy-dependencies</goal></goals>" +
129                 "<configuration>" +
130                     "<outputDirectory>${project.build.outputDirectory}</outputDirectory>" +
131                     "<includeArtifactIds>plugin-license-storage-plugin</includeArtifactIds>" +
132                     "<stripVersion>true</stripVersion>" +
133                 "</configuration>" +
134             "</execution>" +
135         "</executions>";
136     
137     @Override
138     public PluginProjectChangeset createModule(LicensingProperties props) throws Exception
139     {
140         ClassId licenseServletClass = props.getClassId();
141         ServletProperties licenseServletProps = new ServletProperties(licenseServletClass.getFullName());
142         licenseServletProps.setUrlPattern("/" + props.getLicenseServletPath());
143         licenseServletProps.setNameI18nKey(LICENSE_SERVLET_NAME_I18N_KEY);
144         licenseServletProps.setDescriptionI18nKey(LICENSE_SERVLET_DESCRIPTION_I18N_KEY);
145         licenseServletProps.setCreateClass(false);
146         BasicClassModuleProperties licenseServletClassProps = new BasicClassModuleProperties(licenseServletClass.getFullName());
147         licenseServletClassProps.putAll(props);
148         PluginProjectChangeset licenseServlet = new ServletModuleCreator().createModule(licenseServletProps)
149             .with(createClass(licenseServletClassProps, LICENSE_SERVLET_TEMPLATE));
150         
151         PluginProjectChangeset ret = new PluginProjectChangeset()
152             .with(DEPENDENCIES)
153             .with(BUNDLE_INSTRUCTIONS)
154             .with(BUNDLED_ARTIFACTS)
155             .with(mavenPlugin(artifactId(MAVEN_DEPENDENCY_PLUGIN_ID), noVersion(), MAVEN_DEPENDENCY_PLUGIN_CONFIG))
156             .with(pluginParameter("atlassian-licensing-enabled", "true"))
157             .with(COMPONENT_IMPORTS)
158             .with(COMPONENTS)
159             .with(licenseServlet)
160             .with(resourceFile("", "license-admin.vm", fromFile(LICENSE_SERVLET_VELOCITY_TEMPLATE)))
161             .with(createI18nStrings(props, PROPERTIES_TEMPLATE));
162         
163         if (props.includeExamples())
164         {
165             ClassId helloWorldServletClass = props.getClassId().className(HELLO_WORLD_SERVLET_CLASS_NAME);
166             ServletProperties helloWorldServletProps = new ServletProperties(helloWorldServletClass.getFullName());
167             helloWorldServletProps.setUrlPattern("/" + props.getHelloWorldServletPath());
168             helloWorldServletProps.setCreateClass(false);
169             BasicClassModuleProperties helloWorldServletClassProps = new BasicClassModuleProperties(helloWorldServletClass.getFullName());
170             helloWorldServletClassProps.putAll(props);
171             helloWorldServletClassProps.setFullyQualifiedClassname(helloWorldServletClass.getFullName());
172             PluginProjectChangeset helloWorldServlet = new ServletModuleCreator().createModule(helloWorldServletProps)
173                 .with(createClass(helloWorldServletClassProps, HELLO_WORLD_SERVLET_TEMPLATE));
174 
175             ret = ret.with(helloWorldServlet);
176         }
177         
178         return ret;
179     }
180 
181     @Override
182     public String getModuleName()
183     {
184         return MODULE_NAME;
185     }
186 }