Query on Contract JAVA API Hyperledger Fabric - hyperledger-fabric

I am analyzing the Fabcar Java Project in Hyperledger Fabric.
Below are the dependencies in project. snippet from pom.xml
<dependencies>
<dependency>
<groupId>org.hyperledger.fabric</groupId>
<artifactId>fabric-gateway-java</artifactId>
<version>2.1.1</version>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
<version>1.4.2</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.4.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<version>5.4.2</version>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.12.2</version>
<scope>test</scope>
</dependency>
I am able to run the Programs in it. like
EnrollAdmin.main(null);
RegisterUser.main(null);
ClientApp.main(null);
Then I wanted to write my own smart contract from scratch.so started research/analysis and stumbled upon
Video By Hyperledger Foundation
here in this video , presenter said every smart contract has to extend from Contract class.
Contract class contains methods like
beforeTransaction()
afterTransaction()
createContext()
etc
so I searched Contract class in my Java workspace and found different methods present in it.
such as
Transaction createTransaction(String name);
submitTransaction(String name, String... args)
evaluateTransaction(String name, String... args)
So my questions
has Contract class upgraded and I am watching old tutorials or I am referring to OLD APIs ?
Also I found that , there is a dependency fabric-chaincode-shim 2.2.4 API, which contains the methods which matches with Video tutorial.
package: org.hyperledger.fabric.contract
interface: ContractInterface
Do i have to import this dependency or my existing Contract interface(org.hyperledger.fabric.gateway.Contract) enough to write a new smart contract?

There are two coding aspects to writing an application for Fabric. One is the Smart Contract (also referred to as chaincode). The other is the client side application that will send in transaction requests that result in functions in the Smart Contract being executed.
Taking the SmartContract first, that will extend the Contract Interface, and then you'll write various transaction functions (for example createAsset).
To 'run' these transaction functions a client application needs to call submit or evaluate transaction. Only if a transaction function is submitted will changes be made to the ledger. Evaluate is therefore really just a query.
In summary there are two APIs that you need to use - one for the Contract and one for the Client Application.

createContext(), beforeTransaction() and afterTransaction() methods are part of the Hyperledger Fabric contractapi. These methods are available to use in chaincode. createTransaction​(), submitTransaction() and evaluateTransaction() are part of the application gateway. Application gateway package is used to interact with the Fabric network. You can invoke/query chaincode using application gateway. Samples for both are available in fabric-samples. For more information, refer official documentation.

Related

Access extension properties via Microsoft Graph SDK for Java

I have a custom user property implemented via the Extension Property on my directory. The property is visible in the graph explorer:
https://graph.microsoft.com/v1.0/users/6dc2cecc-a3dd-4c18-ba7a-38b70db82e95?$select=extension_8655692d0a034443a9b32af6caa95f56_favColour
returns
{
"#odata.context": "https://graph.microsoft.com/v1.0/$metadata#users(extension_8655692d0a034443a9b32af6caa95f56_favColour)/$entity",
"extension_8655692d0a034443a9b32af6caa95f56_favColour": "foooooo"
}
I can also see it in the raw response in the User object returned by com.microsoft.graph.models.extensions.User#getRawObject when using MS Graph Java SDK, but the User class doesn't seem to have any API for accessing this kind of properties. Best I can do is manipulating the raw response. Is there any better way of accessing extension properties without touching the raw response?
Extension properties can be accessed by ExtensionPropertyCollectionPage class
If you are using User Class, you can try with below code inside the User Class:
final User user = graphClient.me()
.buildRequest()
.select("extension_8655692d0a034443a9b32af6caa95f56_favColour")
.get();
The User entity really doesn't contain the extension property. Then I try to find answer from the c# SDK, as I tested by c# SDK, I can get the extension claim from the User.Result.AdditionalData, so I came back to Java SDK and I found the property additionalDataManager, then I test it and get the result.
User user = graphClient.users("198a7xxxxx19ce1c").buildRequest().get();
String res = user.additionalDataManager().get("extension_3d3xxxxe8707e_tiny_custom_prop").toString();
=================================Update=================================
It's ok for me to get it
UserCollectionPage users = graphClient.users()
.buildRequest().select("extension_3d35xxx7e_tiny_custom_prop")
.get();
String temp = users.getCurrentPage().get(1).additionalDataManager().get("extension_3d35xxx07e_tiny_custom_prop").toString();
And if you find it not working for you, maybe you can try to use beta version for calling the api.
<repositories>
<repository>
<id>sonatype-snapshot</id>
<name>Sonatype Snapshot</name>
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
</repository>
</repositories>
<dependency>
<groupId>com.microsoft.graph</groupId>
<artifactId>microsoft-graph-beta</artifactId>
<version>0.33.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-identity</artifactId>
<version>[1.3,)</version>
</dependency>

Are jaxrs 1.1 (WLP 8.5) annotated methods thread safe?

I am using jaxrs1.1 jar shipped with Websphere liberty profile 8.5 for creating REST WebService.
Lets suppose we have a method addNewProject as shown below :
If many people call this webservice method to add project concurrently. using link below , are there any concurrency issue? In servlet, each request is a separate thread , is it the same case here or should we handle concurrency by ourselves ?
endpointLink: http://somehost.com/path1/path2/addprojectdetails and POST the JSON object.
#POST
#Path("addprojectdetails")
#Consumes(MediaType.APPLICATION_JSON)
#Produces(MediaType.APPLICATION_JSON)
public Response addNewProject(ProjectDetails projectdetailsObj) {
return Response.status(200).entity("Project"+projectdetailsObj.getProjectname()+"successfully added").build();
}
I'm not sure what kind of concurrency issues you might be thinking of. The object itself can be either a singleton or request scoped (if using CDI) or a stateless session bean (if using EJB). If you're using a singleton, then you may need to be thread aware and not store state within the class.
It would probably help to understand what kind of concurrency issues you had in mind to answer more thoroughly.

autofac and multithreading

when doing parallel/multithreading, if there are dependencies that are not thread safe, what kind of instance method can be used with autofac to get an instance per thread? from what I know, autofac is the kind of DI container for certain framework like asp.net/mvc but for the rest of the app type like windows service, it does not have any support. in my scenario, i am doing multithreading for a windows service that also hosting a web api service. what kind of registration can be used so that it will work for web api instanceperhttprequest and instanceperlifetimescope. two separate container?
EDIt:
using this parallel extension method here:
public static Task ForEachAsync<T>(this IEnumerable<T> source, int dop, Func<T, Task> body)
{
return Task.WhenAll(
from partition in Partitioner.Create(source).GetPartitions(dop)
select Task.Run(async delegate
{
using (partition)
{
while (partition.MoveNext())
{
await body(partition.Current);
}
}
}));
}
so the body will be use to do the work. DI will need to be inside of the body func.
It doesn't matter what kind of application you run; the pattern is always the same. You should resolve one object graph per request. In a web application, a request means a web request, in a windows service, a request is usually a timer pulse.
So in a windows service, each 'pulse' you start a new lifetime scope, and within this scope you resolve your root object and call it.
If however, you process items in parallel within a single request, you should see each processed item as a request of its own. So that means that on each thread you should start a new lifetime scope and resolve a sub object graph from that scope and execute that. Prevent passing services that are resolved from your container, from thread to thread. This scatters the knowledge of what is thread-safe, and what isn't throughout the application, instead of keeping that knowledge centralized in the startup path of your application where you compose your object graphs (the composition root).
Take a look at this article about working with dependency injection in multi-threaded applications. It's written for a different DI library, but you'll find most of the advice generically applicable to all DI libraries.

How to handle subflows

Can we use somehow "subflows" in Spring Integration?
I have many different processes which would use the same "subflow". These processes have always the same part which would be good to be put into a separate file.
What would be the corrent way to implement these flows?
I tried to find a solution to use subflows in Spring Integration but I could not find anything.
One simple technique is to put the subflow in a separate file with "well-known" input and output channels (the subflow starts with one channel and ends with another). Then, simply <import/> the subflow and send/consume to/from the input/output channel.
Or, instead of an import you can use the Java DSL to define the subflow and add it to your application contexts that need the subflow...
#Configuration
public class MySubflowDefinition {
#Bean
public IntegrationFlow subflow() {
return IntegrationFlows.from("someInChannel")
.transform(...)
...
.channel("someOutChannel")
.get();
}
}
For a more formal "subflow" definition, see the spring-integration-flow extension. This solution also allows the same subflow to be invoked from multiple places in the same application context.
spring-integration-java-dsl and spring-integration-flow are both available in the spring repo and maven central with (currently) versions 1.0.0.RELEASE.

How does the Social Business Toolkit Samples application uses managed-beans.xml?

So far I have:
installed and started sbt.sample-1.0.0.20140125-1133.ear on my WebSphere Application
Server,
added an URL resource for the SBT Properties file.
The Social Business Toolkit Samples app runs fine and I'm able to connect to my IBM Connections and retrieve some ActivityStream entries.
When I first loaded the application, I noticed this error:
Exception stack trace: com.ibm.websphere.naming.CannotInstantiateObjectException: A NameNotFoundException occurred on an indirect lookup on the name java:comp/env/url/ibmsbt-managedbeansxml. The name java:comp/env/url/ibmsbt-managedbeansxml maps to a JNDI name in deployment descriptor bindings for the application performing the JNDI lookup. Make sure that the JNDI name mapping in the deployment descriptor binding is correct. If the JNDI name mapping is correct, make sure the target resource can be resolved with the specified name relative to the default initial context.
In the Samples application's ibm-web-bnd.xml file I found this line:
<resource-ref name="url/ibmsbt-managedbeansxml" binding-name="url/ibmsbt-managedbeansxml" />
And in the web.xml:
<resource-ref>
<description>Reference to a URL resource which points to the managed bean configuration for the Social Business Toolkit.</description>
<res-ref-name>url/ibmsbt-managedbeansxml</res-ref-name>
<res-type>java.net.URL</res-type>
<res-auth>Container</res-auth>
<res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>
I'm wondering, why should there be an URL resource to the JSF Application Configuration Resource File (managed-beans.xml) in the first place? According to the Java EE documentation the JavaServer Faces implementation will look for it in the /WEB-INF/ folder.
Does the SBT uses JavaServer Faces technology somewhere? Or can I choose not to use the managed-beans.xml file in my own applications that use the SBT?
I wouldn't recommend you consider them related. managed-beans.xml had a prior name, and it's just a set of configuration objects. The project itself does not use Java Server Faces.
I just read the documentation again, more carefully than the first time, and I think I now have a better understanding of what I asked in my second question. From the documentation:
In a web application SBTFilter (HTTP servlet filter) is responsible
for initializing the application using servlet context. Application
does the initialization like loading the managed beans and properties
factories.
The sample app is a web application. I think in my own application I can choose to use com.ibm.commons.runtime.impl.app.ApplicationStandalone instead of com.ibm.commons.runtime.impl.servlet.ApplicationServlet and then configure an endpoint programmatically. Or alternatively do not use an Application at all, like so:
RuntimeFactory runtimeFactory = new RuntimeFactoryStandalone();
Application application = runtimeFactory.initApplication(null);
Context.init(application, null, null);

Resources