Unknown Yesterday

Thursday, June 11, 2020

Initializing git repo and setting remote

I have been creating a lot of git repos recently. I am tired of looking up the same information again and again.

I have been creating a lot of Java projects as I experiment with Spring Boot. After creating a repo, I experiment with it a bit, then decide if I want to keep working in it.

The first step is to change to the project directory and and a .gitignore file. I have been using Java on a Mac and this is the file I use.

HELP.md
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**
!**/src/test/**

### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache

### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr

### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/

### VS Code ###
.vscode/
 
# Compiled class file
*.class

# Log file
*.log

# BlueJ files
*.ctxt

# Mobile Tools for Java (J2ME)
.mtj.tmp/

# Package Files #
*.jar
*.war
*.nar
*.ear
*.zip
*.tar.gz
*.rar

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*

# Mac OS $
.DS_Store


Execute git init in the project folder. This creates the .git folder that is the local repo.

I am using GitHub to store remote copies of the repo. Create a new, empty repo and issue the commands:

git commit -m "first commit"
git remote add origin https://git-username@github.com/git-username/git-repo.git
git push -u origin master
 
After these commands, I can use git push to commit changes.

Monday, June 8, 2020

Creating webapp using Spring Boot and Maven

I created a base Spring Boot project to see all the dependencies I needed for a simple web application.

I created a simple Spring Boot project using the Initializr. It had no extra packages added.

I added the spring-boot-starter-web package, which included tomcat. Tomcat had type 'provided'. If I omitted the version, the war did not work when deployed, but worked locally.

I created a simple home controller using the Controller annotation and the GetMapping annotation.

The default view resolver is to the /src/main/resources/static folder without an extension. I created home.html in the static package and the page was found. Static is copied to WEB-INF/classes.

I wanted to deploy the app to an external tomcat installation. I extended the application class from SpringBootServletInitializer to create a war file and identify the starting application.

I added a view resolver and set the view class to JSTL. I had to add a dependency for jstl. I had to add a webapp folder for the JSPs, /src/main/webapp. The path is to the root of the web app. I did not want my JSPs in the classes folder.

Be sure that the path to the JSTL library is http and not https.

These are all the updated files to get it working:

@SpringBootApplication
public class DemoApplication extends SpringBootServletInitializer {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }             
        @Override
        protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
           return application.sources(DemoApplication.class);
        }
        
        @Bean
        public ViewResolver internalResourceViewResolver() {
            InternalResourceViewResolver bean = new InternalResourceViewResolver();
            bean.setViewClass(JstlView.class);
            bean.setPrefix("/WEB-INF/jsp/");
            bean.setSuffix(".jsp");
            return bean;
        }

}

pom.xml


        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.3.0.RELEASE</version>
            <type>jar</type>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <version>9.0.35</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
index.jsp

<%-- 
    Document   : index
    Created on : Jun 8, 2020, 1:01:38 PM
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

    <c:choose>
        <c:when test="${not empty param.name}">
            <h1>Hello ${param.name}</h1>
        </c:when>
        <c:otherwise>
            <h1>Hello Stranger</h1>
        </c:otherwise>
    </c:choose>
    
</body>
</html>



Wednesday, June 3, 2020

HttpClient Daemon Threads

I have discovered that java.net.http.HttpClient.send leaves daemon threads that do not terminate when interrupted.

I am using
  • Maven 1.6
  • Java 1.14
  • NetBeans 11.3
When I make a simple HTTP request using HttpClient.send using the maven goal exec.java, the code runs, but pauses and I get these warnings after 15 seconds, and then the app stops:

thread Thread[HttpClient-1-Worker-0,5,org.sonatype.mavenbook.ch07.simple.weather.oauth.lone.WeatherYdnJava] was interrupted but is still alive after waiting at least 14997msecs
thread Thread[HttpClient-1-Worker-0,5,org.sonatype.mavenbook.ch07.simple.weather.oauth.lone.WeatherYdnJava] will linger despite being asked to die via interruption
thread Thread[HttpClient-1-Worker-1,5,org.sonatype.mavenbook.ch07.simple.weather.oauth.lone.WeatherYdnJava] will linger despite being asked to die via interruption
thread Thread[HttpClient-1-Worker-2,5,org.sonatype.mavenbook.ch07.simple.weather.oauth.lone.WeatherYdnJava] will linger despite being asked to die via interruption
NOTE: 3 thread(s) did not finish despite being asked to  via interruption. This is not a problem with exec:java, it is a problem with the running code. Although not serious, it should be remedied.
Couldn't destroy threadgroup org.codehaus.mojo.exec.ExecJavaMojo$IsolatedThreadGroup[name=org.sonatype.mavenbook.ch07.simple.weather.oauth.lone.WeatherYdnJava,maxpri=10]


It is interesting that if I use the Play button in NetBeans to run the code, it works without any warnings. NetBeans uses exec:exec instead of exec:java. One difference between exec:java and exec:exec is how daemon threads are handled. With exec:java, after maven is done doing its work, maven sends an interrupt to all the daemon threads, hoping that they will terminate nicely. The exec:java goal also runs the code in the same VM as maven.

The exec:exec goal does not show the warning. I do not know the reason, but it could be that it terminates the threads unceremoniously or it waits for them to finish.

Some properties for the exec:java goal control how daemon threads are handled.
  • daemonThreadJoinTimeout - if I set this to 60 seconds or more, then the daemon threads terminate on there own. I assume that the TCP connection was closed by one side, so the threads terminate.
  • cleanupDaemonThreads - if this is set to false, then the threads are not interrupted and the timeout is not used. I assume that the threads are killed.
  • stopUnresponsiveDaemonThreads - this is a bad idea for this example. If I set this to true, the app will never end. As soon as one is killed, another pops up in its place. I assume this would continue for a minute in this case, waiting for the TCP connection to close.
Conclusion: I will use exec:exec. The downside of using exec:exec is that it creates a separate VM to run the java program. If that becomes a problem, I will set cleanUpDaemonThreads to false in the exec:java goal.

Friday, May 22, 2020

Struggling with Nested Tables and Unit Tests

I am using Spring Boot with Spring Data and attempting to write unit tests.

I have a three tables: Orders have more than one Taco. Tacos have more then one ingredient.

I am attempting to read an Order, with all its Tacos, and the Ingredients for each Taco.

In Order, tacos is a list. In Taco, ingredients is a list.

Here are some good references:
JPQL queries. https://www.objectdb.com/java/jpa/query/jpql/from
EntityGraphs: https://www.radcortez.com/jpa-entity-graphs/
Problems with EntityGraphs: https://stackoverflow.com/questions/31943989/spring-data-jpa-and-namedentitygraphs
JPA LifeCycle: https://www.javabullets.com/jpa-entity-lifecycle/
Baeldung EntityGraph example: https://www.baeldung.com/jpa-entity-graph
Removing old data from cache when testing: https://stackoverflow.com/questions/19057802/junit-hibernate-testing-lazy-associations-fetched
Transactional testing: https://www.marcobehler.com/2014/06/25/should-my-tests-be-transactional 

I have tried many ways to complete the task. The most fruitful was with EntityGraph.

When I attempted the query, I received a Hibernate multiple bag exception.

I researched many articles on the net. One solution is to use a set, but one site cautioned against that, since it must create a Cartesian product of all the tables.
https://vladmihalcea.com/hibernate-multiplebagfetchexception/

I tried to follow the advice, but my example is a little different. I believe the example uses the same approach as nested select statements. I cannot use that idea, since that example is accessing two collections from the same parent table. I want to get all tacos for an order, then get all ingredients for each of those tacos. I cannot perform a join on tacos.ingredients, since tacos is a list. I would have to join on the elements in the list.

I am beginning to think that the best solution is to change to sets instead of lists. Without it, I am forced to do a separate select to get the ingredients for each taco and then add those ingredients to the original result set of the tacos in the order.

Here is a new thought: read all the tacos with their ingredients. Somehow, organize these tacos into orders. Bad idea.

I imagine there is no other way than creating the Cartesian product. I will try with sets.

I attempted to work with sets. The changes were minimal, since many of the methods return Iterable.

I used an EntityGraph with a subgraph for ingredients, but it failed with a null pointer exception.

@NamedEntityGraph(
    name = "Taco_Order.detail",
    attributeNodes = {
        @NamedAttributeNode(value="tacos"
                            ,subgraph="tacos-subgraph"
        )
    }
    ,subgraphs = {
        @NamedSubgraph(
            name="tacos-subgraph",
            attributeNodes= {
                @NamedAttributeNode("ingredients")
            })
    }
    )

If I removed the subgraph, it worked, it even retrieved the ingredients. Confusing.

I removed the entity graph and debugged the code. The tacos were not loaded, as was expected.

I will add the entity graph again and see what happens. It may be a case of stale data in the session. I still got the ingredients. I had added an eager findById to taco, maybe that caused a problem.

I am stuck. I do not know why the ingredients are read.






Tuesday, May 5, 2020

Observations about CHtml::link and regular links

I am using Yii and have links to several controllers.

I had normal links in the page that worked when I proceeded from the base URL. They failed after I clicked the Home button in the menu.

After clicking Home, the URL was .../index.php/site/index, referencing the site controller.

When I click the normal link,

   
  • Unit


  • it tries to access .../index.php/site/Unit and fails.

    When I click the CHtml::link in any of these formats,

       

  •    

  •    


  • it tries to access .../index.php/Unit/index and succeeds.

    When I click the modified CHtml::link,

       


  • it tries to access .../index.php/site/Unit/index and fails.

    The only one that works properly is the CHtml::link with an array for the destination URL.

    The normal URLs add to the existing URL, without noticing that index.php is the actual page and the rest of the URL is path info, so they fail.

    Looking at normalizeUrl and createUrl explain the reasons for the CHtml::links.

    • normalizeUrl is only called if the parameter is an array
    • $this->getId() returns the name of the current controller
    • $this->getAction()->getId() returns the name of the action for the current controller
    • The remaining items are from createUrl:
      • If the route parameters is empty, return the current URL
      • If the route does not contain a /, then replace the current action with / and the route
      • If the leading character in the route is not / and the current module is not null, then replace the current controller with the module and add the route
      • If the route has a site/action or /site, then create the URL from the Yii app, not the current controller
    Conclusion, when creating a link for a different controller, use CHtml::link with an array parameter so that the URL is created based on the Yii app and not the current controller.



      Monday, May 4, 2020

      Yii CActiveRecord Relation to a Relation that is a View

      I have tables for Council, Unit, Offices, CouncilOffices, and ViewWithPhonebook. The view is the records for the unit with additional data retrieved from an online phonebook.

      CouncilOffices implements a many-to-many relationship between Offices and Council. The council are unit members who hold offices in the organization.

      Council Relations

      array(
      'officeRelation'=>array(self::MANY_MANY, 'Offices', 
                                    'council_offices(council_index, offices_index)'
      ), 
      'viewRelation'=>array(self::BELONGS_TO,
                  'ViewPhonebook', 'id'),

      'unitRelation'=>array(self::BELONGS_TO, 
                  'Unit', 'id'),       
      );

      CouncilOffices Relations

      array(
          'councilRelation'=>array(self::BELONGS_TO, 
                                   'Council', 'council_index'),
          'officesRelation'=>array(self::BELONGS_TO, 
                                   'Offices', 'offices_index'),
      );

      Offices Relations

      array(
           'councilRelation'=>array(self::MANY_MANY, 'Council',
                'council_offices(offices_index, council_index)',
            ),
      );

      Unit Relations

      array(
           'unitRelation'=> array(self::HAS_MANY, 'Council', 'id'),);

      ViewWithPhonebook Relations

      return array(
           'viewRelation'=> array(self::HAS_MANY, 'Council', 'id'),);

      The code worked if I used the unit relation. The problem I had was with the view, since it did not have a primary key. If I tried to include the relation, I received this SQL error.
      ON
      (`councilRelation`.`id`=`unitRelation`.``) WHERE (year=:year).
      It is clear that the generated SQL was looking for a primary key, but couldn't find it. I searched for a long time for a solution. I finally found the solution at http://codeinch.com/yii-set-primary-key-in-model/. Add a primaryKey method to the model and return the name of the primary key.

      public function primaryKey() {
          return 'ID';
      }

      Once this was set, I could retrieve the Council relation from Council Offices and then retrieve the View relation from the Council.

      $criteria = new CDbCriteria;
      $criteria->condition = 'year=:year';
      $criteria->params = array(':year'=>'2020');

      $data = CouncilOffices::model()
        ->with(array(
          'councilRelation'
             =>array('select'=>'index, year, id'),        
          'councilRelation.unitRelation'
             =>array('select'=>'ID, Name')))
        ->findAll($criteria);

      Sunday, May 3, 2020

      Mojave VM in Catalina

      I just upgraded to Mac OS Catalina and see that my 32-bit applications are no longer supported.

      I have decided to create a Mojave Virtual Machine to be able to run the 32-bit applications.

      Ironically, my VirtualBox application was 32-bit! I downloaded a new, 64-bit version.

      I followed the steps at https://techsviewer.com/install-macos-10-14-mojave-virtualbox-windows/ to create a Mojave ISO image.

      The same site has instructions for creating the virtual machine on Windows, but I want it on Mac. The first few steps can be easily modified for Mojave, 64-bit MacOS. Follow the steps until you are asked to edit the settings after the virtual machine is created. They did not work on Mac. I picked up the steps after the settings were changed and the command line actions were done.

      I found a separate site that just had me start the vm after it is created. I was prompted for the ISO I created. The process continued to the recovery screen for OS. I clicked Install OS. The Mojave image was found and then I was able to follow the normal installation for Mojave.

      Open VirtualBox and click New.


      Proved details for virtual machine (VM), then click Continue.


      Specify memory size of 2G.


      Specify size of hard drive. Suggested 20GB is too small. Trying 100 GB. Click Create.


      Review settings and click Start.


       Select ISO image for mojave and click Start.


      Select language and click arrow.


      Select Disk Utility from the recovery screen.


      Select the VBOX HARDDISK and click Erase in order to format the drive.


      Create a volume name, choose Mac OS Extended and GUID partition. The GUID is needed for a bootable disk. Click Create.


      Drive is created. Click Done.


      Quit Disk Utility from the File menu and select Install macOS from the recovery menu.


      The mojave.iso file will be used automatically to install OS. Click Continue.


      Accept the license by clicking Agree.


      Confirm agreement by clicking Agree.


      Select the hard drive created earlier and click Install.


      Progress is shown.


      After a while, the Apple logo will appear.


      Something went wrong.


      Shut down machine and restarted. Slow progress at the Apple logo. This is the first Apple logo before the recovery menu appears.



      Recreated another VM with same parameters. I made it to recovery menu, erased drive, and started install. Reached the second Apple screen. Freezes with 16 minutes remaining.


      I found a page for getting stuck at 16 minutes: https://forums.virtualbox.org/viewtopic.php?f=22&t=94495. One recommendation is to set CPUs to 2 and not 4. I had it set for 4 the first time and 1 the second time. Setting it to 2 let me get to the 6 minute mark.


      Still waiting... 2 minutes remaining... less than a minute.


      Next came a reboot and sucess!


      Finished setup, but the desktop is too small.


      I installed the VirtualBox extension pack from https://www.virtualbox.org/wiki/Downloads.

      In the preferences for virtual box, I set the display scale to 200%. macOS does not have guest extensions. https://forums.virtualbox.org/viewtopic.php?f=8&t=91128

      Next was to set up a shared folder. Guest extensions are not available for macOS. Instead, share a folder from Sharing in System Preferences. I followed these instructions to share a folder: https://techsupport.screenplay.com/hc/en-us/articles/360035782711-Creating-a-Shared-Folder-between-your-Mac-running-Catalina-and-a-Virtual-Machine


      I created a folder containing my 32-bit apps and shared it.


      The important information is the smb URL. That will be used to connect to shared folder from VM. In the VM, open Finder and select Connect to Server from the Go menu.

      Enter the smb url.


      Select the volumes to mount.


      I have a folder containing the 32-bit software that does not run in Catalina, but runs in the Mojave VM.


      Some of my apps were so old that they could not run on Mojave! I will abandon them instead of installing another, older macOS VM.














      Followers