how to get the mule application name - mule

Is there some way of getting the base mule application name of a deployed mule application?
The only two options i have found so far is:
muleContext.getConfiguration().getId() //which gives me some not so humanly-readable id of some sort.
and
muleEvent.getFlowConstruct().getName() //gives me that flow name from where this was called.
Each application is in their own application directory when deployed, is it not possible to get a hold of this or some other similar distinguished name from within the muleContext?
kind regards

The simplest way to retrieve the application name is by injecting it into your component using the ${app.name} spring placeholder

This worked for me:
String appName = ((org.mule.module.launcher.MuleApplicationClassLoader)this.getClass().getClassLoader()).getAppName();

For Mule 4.x there are predefined variables and one of those is app.name to retrieve the application name in runtime. This is the link to the Mulesoft documentation to check all the predefined variables:
https://docs.mulesoft.com/mule-runtime/4.2/dataweave-variables-context

For MuleSoft 4.2.0 you can load application name with static Java function:
ClassLoader cl = Class.forName("full-name-holding-this").getClassLoader();
Method getArtifactDescriptor = cl.getClass().getMethod("getArtifactDescriptor");
Object artifactDescriptor = getArtifactDescriptor.invoke(cl);
Method getName = artifactDescriptor.getClass().getMethod("getName");
Object appName = getName.invoke(artifactDescriptor);
return appName.toString();

Related

Default project id in BigQuery Java API

I am performing a query using the BigQuery Java API with the following code:
try (FileInputStream input = new FileInputStream(serviceAccountKeyFile)) {
GoogleCredentials credentials = GoogleCredentials.fromStream(input);
BigQuery bigQuery = BigQueryOptions.newBuilder()
.setCredentials(credentials)
.build()
.getService();
QueryRequest request = QueryRequest.of("SELECT * FROM foo.Bar");
QueryResponse response = bigQuery.query(request);
// Handle the response ...
}
Notice that I am using a specific service account whose key file is given by serviceAccountKeyFile.
I was expecting that the API would pick up the project_id from the key file. But it is actually picking up the project_id from the default key file referenced by the GOOGLE_APPLICATION_CREDENTIALS environment variable.
This seems like a bug to me. Is there a way to workaround the bug by setting the default project explicitly?
Yeah, that doesn't sound right at all. It does sound like a bug. I always just use the export the GOOGLE_APPLICATION_CREDENTIALS environment variable in our applications.
Anyway, you try explicitly setting the project id to see if it works:
BigQuery bigQuery = BigQueryOptions.newBuilder()
.setCredentials(credentials)
.setProjectId("project-id") //<--try setting it here
.build()
.getService();
I don't believe the project is coming from GOOGLE_APPLICATION_CREDENTIALS. I suspect that the project being picked up is the gcloud default project set by gcloud init or gcloud config set project.
From my testing, BigQuery doesn't use a project where the service account is created. I think the key is used only for authorization, and you always have to set a target project. There are a number of ways:
.setProjectId(<target-project>) in the builder
Define GOOGLE_CLOUD_PROJECT
gcloud config set project <target-project>
The query job will then be created in target-project. Of course, your service key should have access to target-project, which may or may not be the same project where your key is created. That is, you can run a query on projects other than the project where your key is created, as long as your key has permission to do so.

mule endpoint builder from java code

The below code fetches the global endpoint named SomeInbound and is stored in an invocation variable named someUrl
<set-variable variableName="someUrl" value="#[app.registry['SomeInbound'].getEndpointBuilder().getEndpoint().getUri()]"/>
How can we do the same thing in java(how to get that app object in java and also the other methods following that)
'app' is just a MEL specific context object for accessing parts of the MuleContext etc. In Java you can grab the registry from the MuleContext like so: muleContext.getRegistry() and then use one of the lookup methods to get the object you want.
TO get the muleCOntext, there any many possible ways. Some of which are:
From a test case.
Injection or the #Lookup
annotation:http://www.mulesoft.org/documentation/display/current/Lookup+Annotation
Implementing MuleContextAware:
https://www.mulesoft.org/docs/site/current3/apidocs/org/mule/api/context/MuleContextAware.html
Or if you're using a Callable component etc. you can get it from the
MuleEventContext : https://www.mulesoft.org/docs/site/current3/apidocs/org/mule/api/MuleEventContext.html#getMuleContext()

Mule ESB: How to call a flow inside Datamapper( script)

I have datamapper, ( source: pojo and target:CSV), I need to call the other flow ( or groovy) inside datamapper. I stuck in passing the parameter to the flow. For example, I don't want entire payload has to go to flow for validation. I need to pass only two values. I used
flowRef(String,Object)
output.Item = flowRef("sampletestFlow",input.Model);
It works fine for single payload. But i have to pass one more parameter ( called input.Policy). I know we have to use
flowRef(String,Object,Map).
But it don't know the format for two input parameter.
Could you please anyone help me on this.
I have handled the scenario by the below way. Have create java class and called the java via damapper script. Below is the code inside datamapper script to call the java code.
stringUtil = new com.test.util.StringUtil();
output.style = stringUtil.formatValue(input.RuleStyle);
Hope this helps.

Get pluginId stored in the IPreferenceNode in Eclipse

I am developing a plugin, in my plugin I want to get another plugin ID. I use the following code:
PreferenceManager pm = PlatformUI.getWorkbench( ).getPreferenceManager();
List<IPreferenceNode> list = pm.getElements(PreferenceManager.PRE_ORDER);
String pluginid;
// restoreDefValues("org.eclipse.ant.ui");
for(IPreferenceNode node : list){
the code to find the node related to the plugin;
}
When I debug the program, I can clearly see that in variable node(IPreferenceNode), it has the value of the pluginId. However, I check the document of IPreferenceNode, it seems that the neither IPreferenceNode nor the class PreferenceNode, provide a method to return the value of pluginId. I tried node.toString() as well, couldn't get the pluginId. So what should I do? Is there any other ways to get a plugin ID from another plugin?
Preference nodes created using the org.eclipse.ui.preferencePages extension point will actually be instances of org.eclipse.ui.internal.dialogs.WorkbenchPreferenceNode. The super class of this (WorkbenchPreferenceExtensionNode) contains the plugin id.
These classes are internal so you should not try to use them directly. However they implement org.eclipse.ui.IPluginContribution which can be used and has a getPluginId() method.
So something like:
if (node instanceof IPluginContribution) {
pluginId = ((IPluginContribution)node).getPluginId();
}
should work.

Getting Path (context root) to the Application in Restlet

I am needing to get the application root within a Restlet resource class (it extends ServerResource). My end goal is trying to return a full explicit path to another Resource.
I am currently using getRequest().getResourceRef().getPath() and this almost gets me what I need. This does not return the full URL (like http://example.com/app), it returns to me /resourceName. So two problems I'm having with that, one is it is missing the schema (the http or https part) and server name, the other is it does not return where the application has been mounted to.
So given a person resource at 'http://dev.example.com/app_name/person', I would like to find a way to get back 'http://dev.example.com/app_name'.
I am using Restlet 2.0 RC3 and deploying it to GAE.
It looks like getRequest().getRootRef().toString() gives me what I want. I tried using a combination of method calls of getRequest().getRootRef() (like getPath or getRelativePart) but either they gave me something I didn't want or null.
Just get the base url from service context, then share it with the resources and add resource path if needed.
MyServlet.init():
String contextPath = getServletContext().getContextPath();
getApplication().getContext().getAttributes().put("contextPath", contextPath);
MyResource:
String contextPath = getContext().getAttributes().get("contextPath");
request.getRootRef() or request.getHostRef()?
The servlet's context is accessible from the restlet's application:
org.restlet.Application app = org.restlet.Application.getCurrent();
javax.servlet.ServletContext ctx = ((javax.servlet.ServletContext) app.getContext().getAttributes().get("org.restlet.ext.servlet.ServletContext"));
String path = ctx.getResource("").toString();