Arquillian : Modifying shrinkwrap webarchive - jboss-arquillian

I am making a restful web service, Service A, that in turn makes a call to another rest service, Service B. Service B is an external resource that is packaged as a war archive. This service contains a regular switchyard file in WEB-INF/switchyard.xml. Sadly, the properties in the switchyard file are rather static, i.e proxy settings, that interfere with my testing. What I would like to do is edit the switchyard.xml file, and remove sections to accommodate unnecessary proxy settings.
Here is how I am creating the deployable for Service B:
#Deployment(name = "my-service", order = 1, testable = false)
public static Archive<?> svcMyDeployable() {
final File file = Maven.configureResolver().fromFile(System.getProperty("settings.xml"))
.loadPomFromFile("pom.xml").resolve("com.sample.service:cool-service:war:?")
.withoutTransitivity().asSingleFile();
final WebArchive war = ShrinkWrap.createFromZipFile(WebArchive.class, file);
final WebArchive archive = ShrinkWrap.create(WebArchive.class, "cool-service.war").merge(war);
return archive;
}
The only way that I can think of overriding the properties of the switchyard.xml file in Service B, is by replacing the WEB-INF/switchyard.xml with a custom one. I can do so as follows:
archive.delete("/WEB-INF/switchyard.xml");
archive.addAsWebInfResource("custom-switchyard/switchyard.xml");
Is there a better way to accomplish this? All that I really need to do is remove 6 lines from the original switchyard.xml file:
<http:proxy>
<http:host>10.10.10.10</http:host>
<http:port>9999</http:port>
<http:user>username</http:user>
<http:password>password</http:password>
</http:proxy>

Related

Spring Cloud Server serving multiple property files for the same application

Lets say I have applicationA that has 3 property files:
-> applicationA
- datasource.properties
- security.properties
- jms.properties
How do I move all properties to a spring cloud config server and keep them separate?
As of today I have configured the config server that will only read ONE property file as this seems to be the standard way. This file the config server picks up seems to be resolved by using the spring.application.name. In my case it will only read ONE file with this name:
-> applicationA.properties
How can I add the other files to be resolved by the config server?
Not possible in the way how you requested. Spring Cloud Config Server uses NativeEnvironmentRepository which is:
Simple implementation of {#link EnvironmentRepository} that uses a SpringApplication and configuration files located through the normal protocols. The resulting Environment is composed of property sources located using the application name as the config file stem (spring.config.name) and the environment name as a Spring profile.
See: https://github.com/spring-cloud/spring-cloud-config/blob/master/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/environment/NativeEnvironmentRepository.java
So basically every time when client request properties from Config Server it creates ConfigurableApplicationContext using SpringApplicationBuilder. And it is launched with next configuration property:
String config = application;
if (!config.startsWith("application")) {
config = "application," + config;
}
list.add("--spring.config.name=" + config);
So possible names for property files will be only application.properties(or .yml) and config client application name that is requesting configuration - in your case applicationA.properties.
But you can "cheat".
In config server configuration you can add such property
spring:
cloud:
config:
server:
git:
search-paths: '{application}, {application}/your-subdirectory'
In this case Config Server will search for same property file names but in few directories and you can use subdirectories to keep your properties separate.
So with configuration above you will be able to load configuration from:
applicationA/application.properies
applicationA/your-subdirectory/application.properies
This can be done.
You need to create your own EnvironmentRepository, which loads your property files.
org.springframework.cloud.config.server.support.AbstractScmAccessor#getSearchLocations
searches for the property files to load :
for (String prof : profiles) {
for (String app : apps) {
String value = location;
if (app != null) {
value = value.replace("{application}", app);
}
if (prof != null) {
value = value.replace("{profile}", prof);
}
if (label != null) {
value = value.replace("{label}", label);
}
if (!value.endsWith("/")) {
value = value + "/";
}
output.addAll(matchingDirectories(dir, value));
}
}
There you could add custom code, that reads the required property files.
The above code matches exactly the behaviour described in the spring docs.
The NativeEnvironmentRepository does NOT access GIT/SCM in any way, so you should use
JGitEnvironmentRepository as base for your own implementation.
As #nmyk pointed out, NativeEnvironmentRepository boots a mini app in order to collect the properties by providing it with - sort of speak - "hardcoded" {appname}.* and application.* supported property file names. (#Stefan Isele - prefabware.com JGitEnvironmentRepository ends up using NativeEnvironmentRepository as well, for that matter).
I have issued a pull request for spring-cloud-config-server 1.4.x, that supports defining additional file names, through a spring.cloud.config.server.searchNames environment property, in the same sense one can do for a single springboot app, as defined in the Externalized Configuration.Application Property Files section of the documentation, using the spring.config.name enviroment property. I hope they review it soon, since it seems many have asked about this feature in stack overflow, and surely many many more search for it and read the currently advised solutions.
It worths mentioning that many ppl advise "abusing" the profile feature to achieve this, which is a bad practice, in my humble opinion, as I describe in this answer

How to include custom web.config files as part of package?

I'm a newbie at SharePoint 2010. I right-click on the project, select Deploy if I want to deploy locally. The Site URL on the project is set to my local machine. If I want to deploy to the QA server, I select Deploy, navigate to the Debug/Release folder, grab the .wsp file, logon to the Central Administration on QA, retract the solution, then do Add-SPsolution <path to wsp file> through powershell, go back to Central Admin, the click on Deploy solution for that package. Works fine.
The web.config on my local machine has a custom connection string, and appsettings. When I deploy the package on the QA server, I'm manually changing the connection string and appsettings specific to QA. I want to automate this process. I want the web.config to be part of the package with it's own custom connecting string (one for local, one for QA, and for Production) and appsettings. How do I do it? The goal is on a new machine, I should be able to deploy the wsp and appsettings+web.config should all be correct without modifying anything manually. How do I accomplish this?
I am pretty sure web.config modifications can't be done with just package files / CAML.
However, what can be done is to deploy a WebApplication Feature Reciever which modifies the web.config through SPWebApplication.WebConfigModifications.
Here is a snippet of code from my project, see the the Code Project KB for more details: (This first bit is just a handy function with some notes.)
// For WebConfigModifications access,
// see http://www.codeproject.com/KB/sharepoint/SPWebConfigModTool.aspx
// Hints:
// app.WebConfigModifications.Add(new SPWebConfigModification
// {
// Type = [add/update child node?]
// Path = [XPath of parent node]
// Name = [XPath to identify child node UNIQUELY]
// Owner = [Use GUID to identify as ours]
// Sequence = [Sequence number, likely 0 for only one]
// Value = [XML node to add/update]
// });
void ModfiyWebConfig (SPWebApplication app, string path, string name, XElement node)
{
app.WebConfigModifications.Add(new SPWebConfigModification
{
Type = SPWebConfigModificationType.EnsureChildNode,
Path = path,
Name = name,
Owner = OwnerId,
Sequence = 0,
Value = node.ToString(),
});
}
Get/init SPWebApplication
var app = properties.Feature.Parent as SPWebApplication;
Queue/setup modifications
ModfiyWebConfig(app,
"configuration/system.webServer/modules",
"add[#name='ASPxHttpHandlerModule']",
new XElement("add",
new XAttribute("name", "ASPxHttpHandlerModule"),
new XAttribute("type", aspxHandlerModule)));
Apply modifications
app.WebService.ApplyWebConfigModifications();
app.Update();

Can I reference a DataContract and its proxy version from same class

I'm dipping my foot into WCF and am trying to make a simple test project that can both consume the service as a service and also directly instantiate it's classes and work with them.
I had an earlier working version where data passed was just primitive types. However, when I attempted to convert to using data contracts, I'm getting conflicts in whether it's referencing the proxy-declared version of the contract or the version from the project itself.
Question: Is it possible to do this and if so, how would I resolve the data contract references?
private void Test()
{
MyService fssDirect = new MyService(); // direct instantiation
MyServiceClient fssService = new MyServiceClient(); // Service proxy
ClientCredentialsContract Client = new ClientCredentialsContract();
ResponseDataContract Result = new ResponseDataContract();
if (CallAsService)
{
Result = fssService.Create(Client, Request);
}
else
{
Result = fssDirect.Create(Client, Request);
}
}
In the above, any reference to the RequestDataContract and ClientCredentialsContract types indicates
Warning: The type 'MyContracts.RequestDataContract' in 'C:...\Test\MyServiceProxy.cs' conflicts with the imported type 'MyContracts.RequestDataContract' in 'C:...\MyService\bin\Debug\Contracts.dll'. Using the type defined in 'C:...\Test\MyServiceProxy.cs'.
(Names changed and paths shortened to protect the innocent)
Thanks,
John
When you create the proxy for your service, try referencing the assembly which contains the data contracts (if using "Add Service Reference", go to the advanced options, select "reuse types in referenced assemblies"; if using svcutil, use the /r argument). This way the tool won't generate the data contracts and you won't have the conflicts.

Update Service Reference Address based on Configuration?

During debugging I added a bunch of service references pointing to services on the Debug machine. Is there any way to automatically regenerate the service references based upon the Configuration? I'd really rather not have to go through and point them all to the Release server when I'm ready to release, then when I need to debug go back and change them all again, etc.
Basicaly, I want the following (done automatically):
Debug -> http://localhost/App/Service1.svc
Release -> http://myserver/Service1.svc
There's no way to do a conditional compilation for configuration. One thing I've used in some projects was to have #if statements in the code which updates the service reference from the config. Something similar to the code below:
static void Main() {
TestClient client = new TestClient();
UpdateAddress(client.Endpoint);
}
static void UpdateAddress(ServiceEndpoint endpoint) {
string address = endpoint.Address.Uri.ToString();
int svcIndex = address.IndexOf(".svc");
int serviceIndex = address.LastIndexOf("/", svcIndex);
address = address.Substring(serviceIndex);
#if DEBUG
address = "http://localhost/App" + address;
#else
address = "http://myserver" + address;
#endif
endpoint.Address = new EndpointAddress(address);
}
Another thing, which I haven't done, but I think may be possible, is to look at the msbuild targets. IIRC you can execute arbitrary commands from msbuild, so you could use a custom target depending on the build configuration, and run some command which would update your config file based on that.
You can use web.config transformations to solve this without code.
http://blogs.msdn.com/b/webdevtools/archive/2009/05/04/web-deployment-web-config-transformation.aspx

NHibernate HybridSessionBuilder, how to switch hibernate cfg based upon url values

I am using the HybridSessionBuilder supplied by Palermo and his team .. link ..
We have our staging environments set up so that the url will be one of the following based on the environment
web-test.company.com
web-cert.company.com
web.company.com
what we normally do is take a look at the url and if it has "-test" we use the test configurations and so on (connection strings, etc).
This is the first project that uses nhibernate in this type of environment. What would be a good way to tell the Session Builder to use the correct hibernate cfg (I will build 1 for each environment).
The HybridSessionBuilder lives in an infrastructure layer and is injected into repositories via StructureMap.
Here's how I select a single configuration file using the HybridSessionBuilder:
public Configuration GetConfiguration()
{
var configuration = new Configuration();
string cfgFile = Path.GetDirectoryName(Assembly.GetAssembly(this.GetType()).CodeBase) +
"\\com.Data.nHibernate.cfg.xml";
configuration.Configure(cfgFile);
configuration.AddAssembly("com.Data");
return configuration;
}
If you want to select configuration files based on the URL I would just identify the call stack that leads to this function and pass in either an enum value or the config file's name directly.