initial remove support
[teamcity/dotNetPackagesSupport.git] / nuget-server / src / jetbrains / buildServer / nuget / server / toolRegistry / impl / NuGetToolManagerImpl.java
1 /*\r
2  * Copyright 2000-2011 JetBrains s.r.o.\r
3  *\r
4  * Licensed under the Apache License, Version 2.0 (the "License");\r
5  * you may not use this file except in compliance with the License.\r
6  * You may obtain a copy of the License at\r
7  *\r
8  * http://www.apache.org/licenses/LICENSE-2.0\r
9  *\r
10  * Unless required by applicable law or agreed to in writing, software\r
11  * distributed under the License is distributed on an "AS IS" BASIS,\r
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
13  * See the License for the specific language governing permissions and\r
14  * limitations under the License.\r
15  */\r
16 \r
17 package jetbrains.buildServer.nuget.server.toolRegistry.impl;\r
18 \r
19 import com.intellij.openapi.diagnostic.Logger;\r
20 import jetbrains.buildServer.nuget.server.toolRegistry.*;\r
21 import org.jetbrains.annotations.NotNull;\r
22 \r
23 import java.lang.reflect.InvocationHandler;\r
24 import java.lang.reflect.Method;\r
25 import java.lang.reflect.Proxy;\r
26 import java.util.Arrays;\r
27 import java.util.Collection;\r
28 import java.util.List;\r
29 \r
30 /**\r
31  * Created by Eugene Petrenko (eugene.petrenko@gmail.com)\r
32  * Date: 11.08.11 1:07\r
33  */\r
34 public class NuGetToolManagerImpl implements NuGetToolManager {\r
35   private static final Logger LOG = Logger.getInstance(NuGetToolManagerImpl.class.getName());\r
36 \r
37   private final AvailableToolsState myAvailables;\r
38   private final NuGetToolsInstaller myInstaller;\r
39   private final ToolsRegistry myInstalled;\r
40 \r
41   public NuGetToolManagerImpl(@NotNull final AvailableToolsState availables,\r
42                               @NotNull final NuGetToolsInstaller installer,\r
43                               @NotNull final ToolsRegistry installed) {\r
44     myAvailables = availables;\r
45     myInstaller = installer;\r
46     myInstalled = installed;\r
47   }\r
48 \r
49   @NotNull\r
50   public Collection<? extends NuGetInstalledTool> getInstalledTools() {\r
51     return myInstalled.getTools();\r
52   }\r
53 \r
54   @NotNull\r
55   public Collection<NuGetInstallingTool> getInstallingTool() {\r
56     return mockInstallingTools();\r
57   }\r
58 \r
59   @NotNull\r
60   public Collection<? extends NuGetTool> getAvailableTools(@NotNull ToolsPolicy policy) throws FetchException {\r
61     //This must be cached to make if work faster!\r
62     return myAvailables.getAvailable(policy);\r
63   }\r
64 \r
65   public void installTool(@NotNull String toolId) {\r
66     myInstaller.installNuGet(\r
67             toolId,\r
68             (InstallLogger) Proxy.newProxyInstance(getClass().getClassLoader(),\r
69                     new Class<?>[]{InstallLogger.class},\r
70                     new InvocationHandler() {\r
71                       public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {\r
72                         StringBuilder sb = new StringBuilder();\r
73                         sb.append(method.getName());\r
74                         for (Object arg : args) {\r
75                           sb.append(" ").append(arg);\r
76                         }\r
77                         LOG.debug(sb.toString());\r
78                         return null;\r
79                       }\r
80                     }));\r
81   }\r
82 \r
83   public void removeTool(@NotNull String toolId) {\r
84     myInstalled.removeTool(toolId);\r
85   }\r
86 \r
87   private List<NuGetInstallingTool> mockInstallingTools() {\r
88     return Arrays.<NuGetInstallingTool>asList(\r
89             new NuGetInstallingTool() {\r
90               @NotNull\r
91               public Collection<String> getInstallMessages() {\r
92                 return Arrays.asList("mgs1", "msg2", "msg3");\r
93               }\r
94 \r
95               @NotNull\r
96               public String getId() {\r
97                 return "ii-1";\r
98               }\r
99 \r
100               @NotNull\r
101               public String getVersion() {\r
102                 return "ii.1.2.43";\r
103               }\r
104             }\r
105     );\r
106   }\r
107 }\r