Repository: incubator-reef
Updated Branches:
  refs/heads/master f9c4d4e5b -> a7b655790


[REEF-494] Make JAR a resource in the client DLL

This addressed the issue by embedding the jars in
`Org.Apache.REEF.Client.dll`. Users do not need to add a JAR nuget
reference anymore.

This also removes the NuGet for the JAR from the "all" NuGet.

JIRA:
  [REEF-494](https://issues.apache.org/jira/browse/REEF-494)

Pull Request:
  This closes #328


Project: http://git-wip-us.apache.org/repos/asf/incubator-reef/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-reef/commit/a7b65579
Tree: http://git-wip-us.apache.org/repos/asf/incubator-reef/tree/a7b65579
Diff: http://git-wip-us.apache.org/repos/asf/incubator-reef/diff/a7b65579

Branch: refs/heads/master
Commit: a7b6557909643a6b7fa6dcc32388c48cf9f49f6d
Parents: f9c4d4e
Author: Beysim Sezgin <[email protected]>
Authored: Fri Jul 31 13:41:05 2015 -0700
Committer: Markus Weimer <[email protected]>
Committed: Thu Aug 6 12:51:57 2015 -0700

----------------------------------------------------------------------
 .../Org.Apache.REEF.All.nuspec                  |   3 +-
 .../Common/DriverFolderPreparationHelper.cs     |  15 +++
 .../Common/ResourceHelper.cs                    |  91 ++++++++++++++++
 .../Org.Apache.REEF.Client.csproj               | 104 +++++++++++++++++--
 .../Properties/Resources.xml                    |  95 +++++++++++++++++
 .../Org.Apache.REEF.Examples.HelloREEF.csproj   |   9 --
 6 files changed, 296 insertions(+), 21 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/a7b65579/lang/cs/Org.Apache.REEF.All/Org.Apache.REEF.All.nuspec
----------------------------------------------------------------------
diff --git a/lang/cs/Org.Apache.REEF.All/Org.Apache.REEF.All.nuspec 
b/lang/cs/Org.Apache.REEF.All/Org.Apache.REEF.All.nuspec
index 96298e0..48c0cd7 100644
--- a/lang/cs/Org.Apache.REEF.All/Org.Apache.REEF.All.nuspec
+++ b/lang/cs/Org.Apache.REEF.All/Org.Apache.REEF.All.nuspec
@@ -36,8 +36,7 @@ under the License.
       <dependency id="Org.Apache.REEF.Driver" version="$version$" />
       <dependency id="Org.Apache.REEF.Network" version="$version$" />
       <dependency id="Org.Apache.REEF.Evaluator" version="$version$" />
-      <dependency id="Org.Apache.REEF.Bridge" version="$version$" />
-      <dependency id="Org.Apache.REEF.Bridge.JAR" version="$version$" />
+      <dependency id="Org.Apache.REEF.Bridge" version="$version$" />      
       <dependency id="Org.Apache.REEF.Examples" version="$version$" />
       <dependency id="Org.Apache.REEF.Client" version="$version$" />
      </dependencies>

http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/a7b65579/lang/cs/Org.Apache.REEF.Client/Common/DriverFolderPreparationHelper.cs
----------------------------------------------------------------------
diff --git 
a/lang/cs/Org.Apache.REEF.Client/Common/DriverFolderPreparationHelper.cs 
b/lang/cs/Org.Apache.REEF.Client/Common/DriverFolderPreparationHelper.cs
index 2bdb9f8..0f1598b 100644
--- a/lang/cs/Org.Apache.REEF.Client/Common/DriverFolderPreparationHelper.cs
+++ b/lang/cs/Org.Apache.REEF.Client/Common/DriverFolderPreparationHelper.cs
@@ -38,6 +38,12 @@ namespace Org.Apache.REEF.Client.Common
     {
         private const string DLLFileNameExtension = ".dll";
         private const string EXEFileNameExtension = ".exe";
+        private const string ClientJarResourceName = "reef_bridge_client";
+        private const string ClientJarFileNameResourceName = 
"ClientJarFullName";
+        private const string DriverJarResourceName = "reef_bridge_driver";
+        private const string DriveJarFileNameResourceName = 
"DriverJarFullName";
+        
+
         private static readonly Logger Logger = 
Logger.GetLogger(typeof(DriverFolderPreparationHelper));
         private readonly AvroConfigurationSerializer _configurationSerializer;
         private readonly REEFFileNames _fileNames;
@@ -116,6 +122,15 @@ namespace Org.Apache.REEF.Client.Common
         /// </summary>
         private void AddAssemblies()
         {
+            var resourceHelper = new 
ResourceHelper(typeof(DriverFolderPreparationHelper).Assembly);
+            var clientJarBytes = 
resourceHelper.GetBytes(ClientJarResourceName);
+            var clientJarFileName = 
resourceHelper.GetString(ClientJarFileNameResourceName);
+            var driverJarBytes = 
resourceHelper.GetBytes(DriverJarResourceName);
+            var driverJarFileName = 
resourceHelper.GetString(DriveJarFileNameResourceName);
+
+            File.WriteAllBytes(clientJarFileName, clientJarBytes);
+            File.WriteAllBytes(driverJarFileName, driverJarBytes);
+
             // TODO: Be more precise, e.g. copy the JAR only to the driver.
             var assemblies = Directory.GetFiles(@".\").Where(IsAssemblyToCopy);
             _fileSets.AddToGlobalFiles(assemblies);

http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/a7b65579/lang/cs/Org.Apache.REEF.Client/Common/ResourceHelper.cs
----------------------------------------------------------------------
diff --git a/lang/cs/Org.Apache.REEF.Client/Common/ResourceHelper.cs 
b/lang/cs/Org.Apache.REEF.Client/Common/ResourceHelper.cs
new file mode 100644
index 0000000..008a236
--- /dev/null
+++ b/lang/cs/Org.Apache.REEF.Client/Common/ResourceHelper.cs
@@ -0,0 +1,91 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+using System;
+using System.Reflection;
+using System.Resources;
+
+namespace Org.Apache.REEF.Client.Common
+{
+    /// <summary>
+    /// Helps with retrieval of embedded resources.
+    /// See Org.Apache.REEF.Client.csproj for embedding resources and use this 
class to retrieve them.
+    /// </summary>
+    internal class ResourceHelper
+    {
+        private const string CouldNotRetrieveResource = "Could not retrieve 
resource '{0}'";
+        private readonly ResourceSet _resourceSet;
+
+        /// <summary>
+        /// Given an assembly, returns a ResourceSet for it.
+        /// </summary>
+        /// <param name="assembly"></param>
+        /// <returns>ResourceSet</returns>
+        internal ResourceHelper(Assembly assembly)
+        {
+            var names = assembly.GetManifestResourceNames();
+            if (null == names[0])
+            {
+                throw new ApplicationException("Could not retrieve Assembly 
Manifest Resource names");
+            }
+            var manifestResources = 
assembly.GetManifestResourceStream(names[0]);
+            if (null == manifestResources)
+            {
+                throw new ApplicationException("Could not retrieve Assembly 
Manifest Resource stream");
+            }
+
+            _resourceSet = new ResourceSet(manifestResources);
+        }
+
+        /// <summary>
+        /// Given resource name, returns corresponding resource
+        /// </summary>
+        /// <param name="resourceName"></param>
+        /// <returns>T</returns>
+        internal T GetResource<T>(string resourceName)
+        {
+            var resource = _resourceSet.GetObject(resourceName);
+            if (null == resource)
+            {
+                throw new 
ApplicationException(string.Format(CouldNotRetrieveResource, resourceName));
+            }
+            return (T) resource;
+        }
+
+        /// <summary>
+        /// Given resource name, returns corresponding resource
+        /// </summary>
+        /// <param name="resourceName"></param>
+        /// <returns>T</returns>
+        internal string GetString(string resourceName)
+        {
+            return GetResource<string>(resourceName);
+        }
+
+        /// <summary>
+        /// Given resource name, returns corresponding resource
+        /// </summary>
+        /// <param name="resourceName"></param>
+        /// <returns>T</returns>
+        internal byte[] GetBytes(string resourceName)
+        {
+            return GetResource<byte[]>(resourceName);
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/a7b65579/lang/cs/Org.Apache.REEF.Client/Org.Apache.REEF.Client.csproj
----------------------------------------------------------------------
diff --git a/lang/cs/Org.Apache.REEF.Client/Org.Apache.REEF.Client.csproj 
b/lang/cs/Org.Apache.REEF.Client/Org.Apache.REEF.Client.csproj
index 2dff64f..d9dacbf 100644
--- a/lang/cs/Org.Apache.REEF.Client/Org.Apache.REEF.Client.csproj
+++ b/lang/cs/Org.Apache.REEF.Client/Org.Apache.REEF.Client.csproj
@@ -27,6 +27,7 @@ under the License.
     <FileAlignment>512</FileAlignment>
     <RestorePackages>true</RestorePackages>
     <SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == 
'*Undefined*'">..</SolutionDir>
+    <TempResxFile Condition="$(TempResxFile) == 
''">$(SolutionDir)bin\$(Platform)\$(Configuration)\$(AssemblyName)\Resources.resx</TempResxFile>
   </PropertyGroup>
   <PropertyGroup>
     <StartupObject />
@@ -56,6 +57,7 @@ under the License.
     <Compile Include="Common\DriverFolderPreparationHelper.cs" />
     <Compile Include="Common\FileSets.cs" />
     <Compile Include="Common\JavaClientLauncher.cs" />
+    <Compile Include="Common\ResourceHelper.cs" />
     <Compile Include="Local\LocalClient.cs" />
     <Compile Include="Local\LocalRuntimeClientConfiguration.cs" />
     <Compile Include="Local\Parameters\DriverConfigurationProviders.cs" />
@@ -108,6 +110,12 @@ under the License.
       <Name>Org.Apache.REEF.Examples</Name>
     </ProjectReference>
   </ItemGroup>
+  <ItemGroup>
+    <EmbeddedResource Include="$(TempResxFile)">
+      <Generator>ResXFileCodeGenerator</Generator>
+      <Visible>false</Visible>
+    </EmbeddedResource>
+  </ItemGroup>
   <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
   <Import Project="$(SolutionDir)\.nuget\NuGet.targets" 
Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" />
   <Target Name="EnsureNuGetPackageBuildImports" 
BeforeTargets="PrepareForBuild">
@@ -118,18 +126,94 @@ under the License.
   </Target>
   <!--begin jar reference-->
   <PropertyGroup>
-    <AfterBuildDependsOn>
-      $(AfterBuildDependsOn);
-      CopyJarFiles;
-    </AfterBuildDependsOn>
+    <BeforeBuildDependsOn>
+      $(BeforeBuildDependsOn);
+      RewriteClientResources;
+    </BeforeBuildDependsOn>
   </PropertyGroup>
-  <Target Name="AfterBuild" DependsOnTargets="$(AfterBuildDependsOn);" />
-  <!--end jar reference-->
-  <!-- To modify your build process, add your task inside one of the targets 
below and uncomment it. 
-       Other similar extension points exist, see Microsoft.Common.targets.
-  <Target Name="BeforeBuild">
+  <!--
+       ########################################################################
+         ReWrite client resx to point to correct version of jars
+       
######################################################################## 
+  -->
+  <!--A Task that extracts the version from the pom in a given folder.-->
+  <UsingTask TaskName="UpdateClientResources" TaskFactory="CodeTaskFactory" 
AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
+    <ParameterGroup>
+      <SnapshotNumber />
+      <ProjectFolder Required="true" />
+      <DebugOrRelease Required="true" />
+      <resxOutputPath Required="true" />
+    </ParameterGroup>
+    <Task>
+      <Reference Include="System.Xml" />
+      <Reference Include="System.Xml.Linq" />
+      <Using Namespace="System" />
+      <Using Namespace="System.IO" />
+      <Using Namespace="System.Xml" />
+      <Using Namespace="System.Linq" />
+      <Using Namespace="System.Xml.Linq" />
+      <Code Type="Fragment" Language="cs"><![CDATA[
+      var Version = XDocument.Load(Path.Combine(ProjectFolder, 
"pom.xml")).Descendants()
+        .Where(x => x.Name.ToString().Contains("version"))
+        .FirstOrDefault().Value;
+      var shortVer = $(RemoveIncubating) ? Version.Replace("-incubating","") : 
Version ;
+      var snapshortNumberAsString = ($(SnapshotNumber) >= 0 && 
$(SnapshotNumber) <=9) ? "0" + $(SnapshotNumber) : $(SnapshotNumber).ToString();
+      var reefVersion = Version;
+
+      var srcDir = @"lang\cs\Org.Apache.REEF.Client\Properties";
+      var binDir = @"lang\cs\bin\x64";
+      var resxInputPath  = Path.Combine(ProjectFolder, srcDir, 
"Resources.xml");      
+      var resourceDir = Path.Combine(ProjectFolder, binDir, DebugOrRelease, 
"Org.Apache.REEF.Bridge.JAR");
+      var byteArrayType = ";System.Byte[], mscorlib, Version=4.0.0.0, 
Culture=neutral, PublicKeyToken=b77a5c561934e089";
+      var jarRest = reefVersion + "-shaded.jar" + byteArrayType;
+
+      var dllResources = new Dictionary<string,string>();
+      dllResources.Add("reef_bridge_client", resourceDir + 
@"\reef-bridge-client-" + jarRest);
+      dllResources.Add("reef_bridge_driver", resourceDir + 
@"\reef-bridge-java-" + jarRest);
+      dllResources.Add("evaluator", resourceDir + 
"Org.Apache.REEF.Evaluator.exe" + byteArrayType);
+      dllResources.Add("ClientJarFullName", "reef-bridge-client-" + 
reefVersion + "-shaded.jar");
+      dllResources.Add("DriverJarFullName", "reef-bridge-java-" + reefVersion 
+ "-shaded.jar");
+      
+      XElement root = XElement.Load(resxInputPath);
+      var resources = root.Descendants().Where(x => x.Name.LocalName == 
"data").ToList();
+      foreach (var resource in resources)
+      {
+          var resourceName = resource.Attribute("name").Value;
+          string replaceValue;
+          if (dllResources.TryGetValue(resourceName, out replaceValue))
+          {
+              var resourceValue = resource.Descendants().Where(x => 
x.Name.LocalName == "value").FirstOrDefault();
+              if (resourceValue != null)
+              {
+                  resourceValue.Value = replaceValue;
+              }
+          }
+      }
+      var settings = new XmlWriterSettings
+      {
+          Indent = true,
+          IndentChars = "  ",
+          NewLineChars = "\r\n",
+          NewLineHandling = NewLineHandling.Replace
+      };
+      using (var wr = XmlWriter.Create(resxOutputPath, settings))
+      {
+          root.WriteTo(wr);
+      }
+        ]]></Code>
+    </Task>
+  </UsingTask>
+  <!--
+      A Target that reWrites client resx to point to correct version of jars
+    -->
+  <Target Name="RewriteClientResources" DependsOnTargets="CopyJarFiles">
+    <UpdateClientResources ProjectFolder="$(REEF_Source_Folder)" 
DebugOrRelease="$(Configuration)" resxOutputPath="$(TempResxFile)">
+    </UpdateClientResources>
   </Target>
   <Target Name="AfterBuild">
+    <Delete Files="$(TempResxFile)" />
+    <RemoveDir Directories="$(BaseIntermediateOutputPath)" />
+  </Target>
+  <Target Name="BeforeBuild" 
DependsOnTargets="$(BeforeBuildDependsOn);RewriteClientResources">
   </Target>
-  -->
 </Project>
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/a7b65579/lang/cs/Org.Apache.REEF.Client/Properties/Resources.xml
----------------------------------------------------------------------
diff --git a/lang/cs/Org.Apache.REEF.Client/Properties/Resources.xml 
b/lang/cs/Org.Apache.REEF.Client/Properties/Resources.xml
new file mode 100644
index 0000000..10172b5
--- /dev/null
+++ b/lang/cs/Org.Apache.REEF.Client/Properties/Resources.xml
@@ -0,0 +1,95 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+Licensed to the Apache Software Foundation (ASF) under one
+or more contributor license agreements.  See the NOTICE file
+distributed with this work for additional information
+regarding copyright ownership.  The ASF licenses this file
+to you under the Apache License, Version 2.0 (the
+"License"); you may not use this file except in compliance
+with the License.  You may obtain a copy of the License at
+
+http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing,
+software distributed under the License is distributed on an
+"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+KIND, either express or implied.  See the License for the
+specific language governing permissions and limitations
+under the License.
+-->
+<!--
+This file is used to embed JAR files to client dll
+-->
+<root>
+  <xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema"; 
xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
+    <xsd:import namespace="http://www.w3.org/XML/1998/namespace"; />
+    <xsd:element name="root" msdata:IsDataSet="true">
+      <xsd:complexType>
+        <xsd:choice maxOccurs="unbounded">
+          <xsd:element name="metadata">
+            <xsd:complexType>
+              <xsd:sequence>
+                <xsd:element name="value" type="xsd:string" minOccurs="0" />
+              </xsd:sequence>
+              <xsd:attribute name="name" use="required" type="xsd:string" />
+              <xsd:attribute name="type" type="xsd:string" />
+              <xsd:attribute name="mimetype" type="xsd:string" />
+              <xsd:attribute ref="xml:space" />
+            </xsd:complexType>
+          </xsd:element>
+          <xsd:element name="assembly">
+            <xsd:complexType>
+              <xsd:attribute name="alias" type="xsd:string" />
+              <xsd:attribute name="name" type="xsd:string" />
+            </xsd:complexType>
+          </xsd:element>
+          <xsd:element name="data">
+            <xsd:complexType>
+              <xsd:sequence>
+                <xsd:element name="value" type="xsd:string" minOccurs="0" 
msdata:Ordinal="1" />
+                <xsd:element name="comment" type="xsd:string" minOccurs="0" 
msdata:Ordinal="2" />
+              </xsd:sequence>
+              <xsd:attribute name="name" type="xsd:string" use="required" 
msdata:Ordinal="1" />
+              <xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" 
/>
+              <xsd:attribute name="mimetype" type="xsd:string" 
msdata:Ordinal="4" />
+              <xsd:attribute ref="xml:space" />
+            </xsd:complexType>
+          </xsd:element>
+          <xsd:element name="resheader">
+            <xsd:complexType>
+              <xsd:sequence>
+                <xsd:element name="value" type="xsd:string" minOccurs="0" 
msdata:Ordinal="1" />
+              </xsd:sequence>
+              <xsd:attribute name="name" type="xsd:string" use="required" />
+            </xsd:complexType>
+          </xsd:element>
+        </xsd:choice>
+      </xsd:complexType>
+    </xsd:element>
+  </xsd:schema>
+  <resheader name="resmimetype">
+    <value>text/microsoft-resx</value>
+  </resheader>
+  <resheader name="version">
+    <value>2.0</value>
+  </resheader>
+  <resheader name="reader">
+    <value>System.Resources.ResXResourceReader, System.Windows.Forms, 
Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
+  </resheader>
+  <resheader name="writer">
+    <value>System.Resources.ResXResourceWriter, System.Windows.Forms, 
Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
+  </resheader>
+  <data name="ClientJarFullName" xml:space="preserve">
+    <value>reef-bridge-client-0.12.0-incubating-SNAPSHOT-shaded.jar</value>
+  </data>
+  <data name="DriverJarFullName" xml:space="preserve">
+    <value>reef-bridge-java-0.12.0-incubating-SNAPSHOT-shaded.jar</value>
+  </data>
+  <assembly alias="System.Windows.Forms" name="System.Windows.Forms, 
Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
+  <data name="reef_bridge_client" type="System.Resources.ResXFileRef, 
System.Windows.Forms">
+    
<value>E:\src\reef\lang\cs\bin\x64\Debug\Org.Apache.REEF.Bridge.JAR\reef-bridge-client-0.12.0-incubating-SNAPSHOT-shaded.jar;System.Byte[],
 mscorlib, Version=4.0.0.0, Culture=neutral, 
PublicKeyToken=b77a5c561934e089</value>
+  </data>
+  <data name="reef_bridge_driver" type="System.Resources.ResXFileRef, 
System.Windows.Forms">
+    
<value>E:\src\reef\lang\cs\bin\x64\Debug\Org.Apache.REEF.Bridge.JAR\reef-bridge-java-0.12.0-incubating-SNAPSHOT-shaded.jar;System.Byte[],
 mscorlib, Version=4.0.0.0, Culture=neutral, 
PublicKeyToken=b77a5c561934e089</value>
+  </data>
+</root>
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/a7b65579/lang/cs/Org.Apache.REEF.Examples.HelloREEF/Org.Apache.REEF.Examples.HelloREEF.csproj
----------------------------------------------------------------------
diff --git 
a/lang/cs/Org.Apache.REEF.Examples.HelloREEF/Org.Apache.REEF.Examples.HelloREEF.csproj
 
b/lang/cs/Org.Apache.REEF.Examples.HelloREEF/Org.Apache.REEF.Examples.HelloREEF.csproj
index f4db762..18def0f 100644
--- 
a/lang/cs/Org.Apache.REEF.Examples.HelloREEF/Org.Apache.REEF.Examples.HelloREEF.csproj
+++ 
b/lang/cs/Org.Apache.REEF.Examples.HelloREEF/Org.Apache.REEF.Examples.HelloREEF.csproj
@@ -70,13 +70,4 @@
     </ProjectReference>
   </ItemGroup>
   <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
-  <!--begin jar reference-->
-  <PropertyGroup>
-    <AfterBuildDependsOn>
-      $(AfterBuildDependsOn);
-      CopyJarFiles;
-    </AfterBuildDependsOn>
-  </PropertyGroup>
-  <Target Name="AfterBuild" DependsOnTargets="$(AfterBuildDependsOn);" />
-  <!--end jar reference-->
 </Project>
\ No newline at end of file

Reply via email to